AJAX PHP EXAMPLE

File "lastname.php" web service makes suggestions of surnames

<?php $arr[]="smith"; $arr[]="johnson"; $arr[]="williams"; $arr[]="jones"; $arr[]="brown"; $arr[]="davis"; $arr[]="miller"; $arr[]="wilson"; $arr[]="moore"; $arr[]="taylor"; $arr[]="anderson"; $arr[]="thomas"; $arr[]="jackson"; $arr[]="white"; $arr[]="harris"; $arr[]="martin"; $arr[]="thompson"; $arr[]="garcia"; $arr[]="martinez"; $arr[]="robinson"; $arr[]="clark"; $arr[]="rodriguez"; $arr[]="lewis"; $arr[]="lee"; $arr[]="walker"; $arr[]="hall"; $arr[]="allen"; $arr[]="young"; $arr[]="hernandez"; $arr[]="king"; $query=$_REQUEST["query"]; $response=""; if ($query !== "") { $query=strtolower($query); $len=strlen($query); foreach($arr as $name) { if (stristr($query, substr($name,0,$len))) { if ($response==="") { $response=$name; } else { $response .= ", $name"; } } } } echo $response==="" ? "no suggestion" : $response; ?>

Website "show.htm" showing hints of surnames in the letter to say.

<!DOCTYPE html> <html> <head> <script> function searchResponse(param) { var xmlhttp; if (param.length==0) { document.getElementById("showResponse").innerHTML=""; 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("showResponse").innerHTML=xmlhttp.responseText; } } xmlhttp.open("GET","lastname.php?query="+param,true); xmlhttp.send(); } </script> </head> <body> <h4>Start typing a lastname </h4> <form action=""> Last name: <input type="text" onkeyup="searchResponse(this.value)" /> </form> <p>Suggestions: <span id="showResponse"></span></p> </body> </html>