Как можно сделать жывой поиск вывести ответ на input в javascript + ajax?
Код:
<script>
function showResult(str) {
if (str.length==0) {
document.getElementById("name[]").value="";
return;
}
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (this.readyState==4 && this.status==200) {
document.getElementById("name[]").value=this.responseText;
}
}
xmlhttp.open("GET","livesearch.php?id="+str,true);
xmlhttp.send();
}
</script>
function showResult(str) {
if (str.length==0) {
document.getElementById("name[]").value="";
return;
}
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (this.readyState==4 && this.status==200) {
document.getElementById("name[]").value=this.responseText;
}
}
xmlhttp.open("GET","livesearch.php?id="+str,true);
xmlhttp.send();
}
</script>
Код:
<table>
<form method="POST" action="action.php">
<tr>
<td><input name="id[]" type="text" onkeyup="showResult(this.value)" value="" /></td>
<td><input type="text" name="name[]" /></td>
</tr>
</form>
</table>
<form method="POST" action="action.php">
<tr>
<td><input name="id[]" type="text" onkeyup="showResult(this.value)" value="" /></td>
<td><input type="text" name="name[]" /></td>
</tr>
</form>
</table>
Код:
<?
$q=$_GET["q"];
$result = mysql_query("SELECT * FROM `table` WHERE id LIKE '%$q%' ");
while ($row = mysql_fetch_array($result))
{
echo $row["name"] ;
}
?>
$q=$_GET["q"];
$result = mysql_query("SELECT * FROM `table` WHERE id LIKE '%$q%' ");
while ($row = mysql_fetch_array($result))
{
echo $row["name"] ;
}
?>