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 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.
Example
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 -
<html> <head> <style> div { width:240px; color:green; } </style> <script> function showResult(str) { if (str.length == 0) { document.getElementById("livesearch").innerHTML = ""; document.getElementById("livesearch").style.border = "0px"; return; } 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"; } } xmlhttp.open("GET","livesearch.php?q="+str,true); xmlhttp.send(); } </script> </head> <body> <form> <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> </form> </body> </html>
READ: PHP AJAX XML Parser
livesearch.php
The livesearch.php is used for calling the data from the xml file and it will send the result to web browser.
<?php $xmlDoc = new DOMDocument(); $xmlDoc->load("autocomplete.xml"); $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; ?>
READ: PHP MySQL Login
autocomplete.xml
It contains autocomplete data and can be accessed by livesearch.php based on title field and Url field -
<pages> <link> <title>HTML</title> <url>https://www.webdesigntutorialz.com/html/index.htm</url> </link> <link> <title>CSS</title> <url>https://www.webdesigntutorialz.com/css/index.htm</url> </link> <link> <title>JavaScript</title> <url>https://www.webdesigntutorialz.com/Javascript/index.htm</url> </link> <link> <title>jQuery</title> <url>https://www.webdesigntutorialz.com/jquery/index.htm </url> </link> <link> <title>WorldPress</title> <url>https://www.webdesigntutorialz.com/worldpress/index.htm </url> </link> <link> <title>Python</title> <url>https://www.webdesigntutorialz.com/python/index.htm </url> </link> <link> <title>AJAX</title> <url>https://www.webdesigntutorialz.com/ajax/index.htm </url> </link> <link> <title>nodejs</title> <url>https://www.webdesigntutorialz.com/nodejs/index.htm </url> </link> </pages>
Output
When the above code is executed, it will produce the following result -
READ: AJAX Live Search in PHP
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.
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.