April 18, 2016
How to Search a PHP Array Using AJAX

Hi, friends in this tutorial I show you how to search a PHP array using Ajax. Below code opens a file, name called as search.php by using with GET method, so we want to create a file, name called as search.php in the same directory and output will be attached with “src”. It contained an array of country names and it displays the value to the web page.
test.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
<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> <h1>Search</h1> <input type="text" size="50" onKeyUp="showResult(this.value)"> <div id="livesearch"></div> </form> </body> </html> <html> <head> <script> function searchme(str) { if(str.length == 0){ document.getElementById("src").innerHTML = ""; return; } else{ var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { document.getElementById("src").innerHTML = xmlhttp.responseText; } }; xmlhttp.open("GET", "search.php?a=" + str, true); xmlhttp.send(); } } </script> </head> <body> <h1>Search</h1> <form> <input type="text" onKeyUp="searchme(this.value)"> </form> <p>Results<span id="src"></span></p> </body> </html> |
search.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
<?php // Array with names $arr [] = "United States"; $arr [] = "United Kingdom"; $arr [] = "Russia"; $arr [] = "Canada"; $arr [] = "India"; $arr [] = "Australia"; $arr [] = "China"; $arr [] = "Argentina"; $arr [] = "Singapore"; // get the q parameter from URL $a = $_REQUEST["a"]; $search = ""; // lookup all hints from array if $q is different from "" if ($a !== "") { $a = strtolower($a); $len=strlen($a); foreach($arr as $name) { if (stristr($a, substr($name, 0, $len))) { if ($search === "") { $search = $name; } else { $search .= ", $name"; } } } } // Output "No Result" if no search was found or output correct values echo $search === "" ? "No result" : $search; ?> |
Download
Mraj
Creative Designer & Developer specialist by the spirit and a loving blogger by thoughts. If you have any questions let me drop an email with the article name to the following email id: [email protected]