Live Search / AutoComplete Using XML & PHP

index.php:-
<html>
<head>
<script type=”text/javascript”>
function showResult(str)
{
if (str.length==0)
  {
  document.getElementById(“livesearch”).innerHTML=””;
  document.getElementById(“livesearch”).style.border=”0px”;
  return;
  }
if (window.XMLHttpRequest)
  {xmlhttp=new XMLHttpRequest();
  }
else
  {xmlhttp=new ActiveXObject(“Microsoft.XMLHTTP”);
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById(“livesearch”).innerHTML=xmlhttp.responseText;
    document.getElementById(“livesearch”).style.border=”1px solid #A5ACB2″;
    }
  }
xmlhttp.open(“GET”,”livesearch.php?q=”+str,true);
xmlhttp.send();
}
</script>
</head>
<body>

<form>
<input type=”text” size=”30″ onKeyUp=”showResult(this.value)” />
<div id=”livesearch”></div>
</form>

</body>
</html>

links.xml:-

<?xml version=”1.0″ encoding=”ISO-8859-1″?>

<pages>
<link>
<webpage>gsivaprabu</webpage>
<pageurl>http://www.gsivaprabu.blogspot.com</pageurl>
</link>
<link>
<webpage>facebook</webpage>
<pageurl>http://www.facebook.com</pageurl>
</link>
<link>
<webpage>Megaupload</webpage>
<pageurl>http://wwwstatic.megaupload.com/</pageurl>
</link>
<link>
<webpage>Rapidshare</webpage>
<pageurl>http://www.rapidshare.com</pageurl>
</link>
<link>
<webpage>Google</webpage>
<pageurl>http://www.google.com</pageurl>
</link>
<link>
<webpage>Yahoo</webpage>
<pageurl>http://www.yahoo.com</pageurl>
</link>
<link>
<webpage>Sphiderplus</webpage>
<pageurl>http://www.sphiderplus.zxq.net</pageurl>
</link>
<link>
<webpage>Gmail</webpage>
<pageurl>http://www.gmail.com</pageurl>
</link>
</pages>

livesearch.php:-

<?php
$xmlDoc=new DOMDocument();
$xmlDoc->load(“links.xml”);

$x=$xmlDoc->getElementsByTagName(‘link’);

$q=$_GET[“q”];

if (strlen($q)>0)
{
$hint=””;
for($i=0; $i<($x->length); $i++)
  {
  $y=$x->item($i)->getElementsByTagName(‘webpage’);
  $z=$x->item($i)->getElementsByTagName(‘pageurl’);
  if ($y->item(0)->nodeType==1)
    {
    if (stristr($y->item(0)->childNodes->item(0)->nodeValue,$q))
      {
      if ($hint==””)
        {
        $hint=”<a href='” .
        $z->item(0)->childNodes->item(0)->nodeValue .
        “‘ target=’_blank’>” .
        $y->item(0)->childNodes->item(0)->nodeValue . “</a>”;
        }
      else
        {
        $hint=$hint . “<br /><a href='” .
        $z->item(0)->childNodes->item(0)->nodeValue .
        “‘ target=’_blank’>” .
        $y->item(0)->childNodes->item(0)->nodeValue . “</a>”;
        }
      }
    }
  }
}

if ($hint==””)
  {
  $response=”No suggestion dear lets try like gsivaprabu”;
  }
else
  {
  $response=$hint;
  }
echo $response;
?>