Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

PHP instance AJAX real-time search


May 11, 2021 PHP


Table of contents


PHP Instances - AJAX Real-Time Search


When using PHP, AJAX provides users with a more user-friendly and interactive search experience. This section gives a specific introduction!


AJAX Live Search

In the following example, we'll demonstrate a real-time search that will get search results as you type your data.

Real-time search has many advantages over traditional search:

  • As you type the data, the matching results are displayed
  • The results are filtered as you continue typing data
  • If the result is too little, you can get a wider range by deleting characters

Search the W3CSchool page in the text box below

The results in the above example are found in an XML .xml file (links). To make this example small and simple, we only provide 6 results.


Instance Interpretation - HTML page

When the user types characters in the input box above, the "showResult()" function is executed. The function is triggered by the "onkeyup" event:

 <html>
 <head>
 <script>
 function showResult(str)
 {
 if (str.length==0)
 { 
 document.getElementById("livesearch").innerHTML="";
 document.getElementById("livesearch").style.border="0px";
 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("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> 

Source code interpretation:

If the input box is empty (str.length,0), the function emptys the contents of the livesearch placeholder and exits the function.

If the input box is not empty, showResult() follows these steps:

  • Create an XMLHttpRequest object
  • Create a function that executes when the server response is ready
  • Send a request to a file on the server
  • Note the parameters (q) added to the end of the URL (containing the contents of the input box)

PHP file

The server page called by JavaScript above is a PHP file .php "Livesearch".

The source code .php "Livesearch" searches the XML file for the title that matches the search string and returns the result:

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

 $x=$xmlDoc->getElementsByTagName('link');

 //get the q parameter from URL
 $q=$_GET["q"];

 //lookup all links from the xml file if length of q>0
 if (strlen($q)>0)
 {
 $hint="";
 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)
 {
 //find a link matching the search 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 output to "no suggestion" if no hint were found
 // or to the correct values
 if ($hint=="")
 {
 $response="no suggestion";
 }
 else
 {
 $response=$hint;
 }

 //output the response
 echo $response;
 ?> 

If JavaScript sends any text (i.e. strlen($q) and 0), it will occur:

  • Load the XML file to the new XML DOM object
  • Traverse all the elements to find the text that matches JavaScript
  • Set the $response URL and title in the "Called" variable. If more than one match is found, all matches are added to the variable.
  • If no match is found, set $response variable to "no suggestion."