PHP Example - AJAX Live Search

AJAX are use for make more easy to understand and intelligent searches.

AJAX Live Search

The following model will show a live pursuit, where you get indexed lists while you type.

Live seek has numerous advantages contrasted with conventional searching:

  • Results are appeared as you quick possable type
  • Results limited as you keep composing with no error
  • If results turn out to be excessively limited, expel characters to see a more extensive result

Search for a welookups page in the information field below:

The results in the model above are found in a XML document (links.xml). To make this model little and straightforward, just six outcomes are available.

Example Explained - The HTML Page

When a client types a character in the information field over, the capacity "showResult()" is executed. The capacity is activated by the "onkeyup" event:

work showResult(str) {
  on the off chance that (str.length==0) {
  on the off chance that (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("livesearch").style.border="1px strong #A5ACB2";

<input type="text" size="30" onkeyup="showResult(this.value)">
<div id="livesearch"></div>


Source code explanation:

If the information field is vacant (str.length==0), the capacity clears the substance of the livesearch placeholder and ways out the function.

If the information field isn't vacant, the showResult() work executes the following:

  • Create a XMLHttpRequest object
  • Create the capacity to be executed when the server reaction is ready
  • Send the demand off to a record on the server
  • Notice that a parameter (q) is added to the URL (with the substance of the input field)

The PHP File

The page on the server called by the JavaScript above is a PHP document called "livesearch.php".

The source code in "livesearch.php" looks a XML record for titles coordinating the hunt string and returns the result:

$xmlDoc=new DOMDocument();


/get the q parameter from URL

/query all connections from the xml record if length of q>0
in the event that (strlen($q)>0) {
  for($i=0; $i<($x->length); $i++) {
    $y=$x->item($i)- >getElementsByTagName('title');
    $z=$x->item($i)- >getElementsByTagName('url');
    if ($y->item(0)- >nodeType==1) {
      //discover a connection coordinating the inquiry text
      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>";

/Set yield to "no suggestion" if no insight was found
/or to the right values
on the off chance that ($hint=="") {
  $response="no suggestion";
} else {

/yield the response
reverberation $response;

If there is any content sent from the JavaScript (strlen($q) > 0), the accompanying happens:

  • Load a XML document into another XML DOM object
  • Loop through all <title> components to discover matches from the content sent from the JavaScript
  • Sets the right url and title in the "$response" variable. On the off chance that more than one match is discovered, all matches are added to the variable
  • If no matches are discovered, the $response variable is set to "no suggestion"