autocomplete by ajax php mysql
test.php
<html>
<head>
<script type="text/javascript">
function callajax()
{
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 (xmlhttp.readyState==4 && xmlhttp.status==200)
{
var customarray=new Array();
var str=xmlhttp.responseText;
customarray=str.split(",");
var obj = actb(document.getElementById('tb_name'),customarray);
alert(customarray[0]);
}
}
var username=document.getElementById('tb_name').value;
xmlhttp.open("POST","fetchvalue.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("userid="+username);
}
</script>
<script language="javascript" type="text/javascript" src="actb.js"></script>
<script language="javascript" type="text/javascript" src="common.js"></script>
</head>
<body>
<form action='' method='post'>
<table>
<tr>
<td><input type="text" name="tb_name" onkeyup="callajax();" id='tb_name'/></td>
</tr>
</table>
</form>
</body>
</html>
<script>
</script>
fetchvalue.php
$link = mysql_connect($host, $user, $pass);
if (!mysql_select_db($db, $link)) {
echo 'Could not select database';
exit;
}
$uid=$_POST['userid'];
//$uid='a';
$select=mysql_query("select * from login where user LIKE '%$uid%'");
?>
<?php
while($f=mysql_fetch_array($select)){
echo $f['user'].',';
}
?>
its work fine but we have to use this alert(customarray[0]);
plz help me..
I strongly discourage to use custom made solution when there are pretty good alternative already builted.
See jQuery UI autocomplete here http://jqueryui.com/demos/autocomplete/
精彩评论