Wednesday, September 26, 2012

Dynamic search in php using ajax

index.php

<?
mysql_connect("localhost","root","");
mysql_select_db("gpsemca");


?>


<html>
<head>
<script type="text/javascript">
function showHint(str)
{
if (str.length==0)
  {
  document.getElementById("txtHint").innerHTML="";
  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 (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","gethint.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>


<form>
Search Keyword: <input type="text" onKeyUp="showHint(this.value)" size="20" />
</form>
<p><span id="txtHint"></span></p>

</body>
</html>

gethint.php

<?
$find=$_GET['q'];
mysql_connect("localhost","root","");
mysql_select_db("gpsemca");

$query=mysql_query("SELECT * FROM details WHERE title LIKE  '%$find%' limit 5 " );
while($row=mysql_fetch_array($query))
{
?>
<a href="<?=$row['url']?>"><?=$row[1]?></a>
<br/>
<? }
?>

No comments:

Post a Comment

Thank you for your Comment....

Popular Posts