Here Sample code of Ajax based search engine with PHP and XML
This is the Javascript Code :
/* <--------------------------> */ /* 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+'&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->getElementsByTagName('NAME'); for ($i=0; $i<=$x->length-1; $i++) { //Process only element nodes if ($x->item($i)->nodeType==1) { if ($x->item($i)->childNodes->item(0)->nodeValue == $q) { $y=($x->item($i)->parentNode); } } } $cd=($y->childNodes); for ($i=0;$i<$cd->length;$i++) { //Process only element nodes if ($cd->item($i)->nodeType==1) { echo($cd->item($i)->nodeName); echo(": "); echo($cd->item($i)->childNodes->item(0)->nodeValue); echo(""); } } ?> |
Tags: Ajax, HTML, JAVASCRIPT, PHP, XML