Ajax Based Search Engine With PHP and XML

Posted By Venu Thomas

Here Sample code of Ajax based search engine with PHP and XML

live_preview Live Preview          downloadDownload Code

 

This is the Javascript Code :

?View Code JAVASCRIPT
/* <--------------------------> */
/* XMLHTTPRequest Enable		*/
/* <--------------------------> */
function createObject() {
var request_type;
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer"){
request_type = new ActiveXObject("Microsoft.XMLHTTP");
}else{
request_type = new XMLHttpRequest();
}
return request_type;
}
 
var http = createObject();
 
/* <--------------------------> */
/* 		SEARCH				   */
/* <--------------------------> */
 
function fnSearchName() {
 
txtInput=encodeURI(document.getElementById('txtInput').value);
 
document.getElementById('divmsg').style.display = "block";
document.getElementById('divmsg').innerHTML = "Searching for <strong>" + txtInput+"</strong>";
// Set te random number to add to URL request
nocache = Math.random();
http.open('get', 'php_Search.php?name='+txtInput+'&amp;nocache = '+nocache);
 
http.onreadystatechange = fnSearchNameReply;
http.send(null);
}
function fnSearchNameReply() {
 
	var response = http.responseText;
	document.getElementById('divResult').innerHTML=response;
 
}

This is the PHP Code :

load("NameList.xml");
$x=$xmlDoc-&gt;getElementsByTagName('NAME');
for ($i=0; $i&lt;=$x-&gt;length-1; $i++)
{
//Process only element nodes
if ($x-&gt;item($i)-&gt;nodeType==1)
  {
  if ($x-&gt;item($i)-&gt;childNodes-&gt;item(0)-&gt;nodeValue == $q)
    {
    $y=($x-&gt;item($i)-&gt;parentNode);
    }
  }
}
$cd=($y-&gt;childNodes);
for ($i=0;$i&lt;$cd-&gt;length;$i++)
{
//Process only element nodes
if ($cd-&gt;item($i)-&gt;nodeType==1)
  {
  echo($cd-&gt;item($i)-&gt;nodeName);
  echo(": ");
  echo($cd-&gt;item($i)-&gt;childNodes-&gt;item(0)-&gt;nodeValue);
  echo("");
  }
}
 
?&gt;

Tags: , , , ,

  •  
  • Share with others:

    Thanks for visiting us! If you enjoyed these icons please feel free to share them! Or if you want to know what's going on with WiseCodes.com, follow us!

    Leave a Reply

    Anti-Spam Protection by WP-SpamFree

    Locations of visitors to this page eXTReMe Tracker