We now have a youtube channel. Subscribe!

PHP AJAX Autocomplete Search Box

PHP AJAX Autocomplete Search Box

Hello folks! welcome back to a new edition of our tutorial on PHP. In this edition of our PHP tutorial, we will be studying about the PHP AJAX Auto complete search.

The Auto complete search box provides a suggestion when you enter a data into the field. In this tutorial, we are using xml to call the auto complete suggestions.


The following example below shows how to Implement the auto complete text box using AJAX and PHP.

Index page

Index page should be as follows -

         div {
         function showResult(str) {
            if (str.length == 0) {
               document.getElementById("livesearch").innerHTML = "";
               document.getElementById("livesearch").style.border = "0px";
            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";
         <h2>Enter Course Name</h2>
         <input type = "text" size = "30" onkeyup = "showResult(this.value)">
         <div id = "livesearch"></div>
         <a href = "https://www.webdesigntutorialz.com">More Details </a>


The livesearch.php is used for calling the data from the xml file and it will send the result to web browser.

   $xmlDoc = new DOMDocument();
   $x = $xmlDoc->getElementsByTagName('link');
   $q = $_GET["q"];
   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) {
            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 = "Please enter a valid name";
   }else {
      $response = $hint;
   echo $response;


It contains autocomplete data and can be accessed by livesearch.php based on title field and Url field -





      <url>https://www.webdesigntutorialz.com/jquery/index.htm </url>

      <url>https://www.webdesigntutorialz.com/worldpress/index.htm </url>

      <url>https://www.webdesigntutorialz.com/python/index.htm </url>

      <url>https://www.webdesigntutorialz.com/ajax/index.htm </url>

      <url>https://www.webdesigntutorialz.com/nodejs/index.htm </url>



When the above code is executed, it will produce the following result -

Alright guys! This is where we are going to be rounding up for this tutorial post. In our next tutorial, we are going to be discussing about the AJAX RSS Feed.

Feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.
© 2023 ‧ WebDesignTutorialz. All rights reserved. Developed by Jago Desain