日期:2014-05-16 浏览次数:20818 次
<html> <head> <script src="clienthint.js"></script> </head> <body> <form> First Name: <input type="text" id="txt1"> <input type="button" value="Submit" onClick="showHint()"> </form> <p>Suggestions: <span id="txtHint"></span></p> </body> </html>
var xmlHttp function showHint() { var str = document.getElementById("txt1").value; if (str.length==0) { document.getElementById("txtHint").innerHTML=""; return; } xmlHttp=GetXmlHttpObject(); if (xmlHttp==null) { alert ("Browser does not support HTTP Request"); return; } var url="gethint.php"; var content="qt="+str; content=content+"&sid="+Math.random(); xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");//for post xmlHttp.onreadystatechange=stateChanged; xmlHttp.open("POST",url,true); xmlHttp.send(content); } function stateChanged() { if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { document.getElementById("txtHint").innerHTML=xmlHttp.responseText; } } function GetXmlHttpObject() { var xmlHttp=null; try { // Firefox, Opera 8.0+, Safari xmlHttp=new XMLHttpRequest(); } catch (e) { // Internet Explorer try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } } return xmlHttp; }
<?php // Fill up array with names $a[]="Anna"; $a[]="Brittany"; $q = $_POST["qt"]; if (strlen($q) > 0) { $hint=""; for($i=0; $i<count($a); $i++) { if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q)))) { if ($hint=="") { $hint=$a[$i]; } else { $hint=$hint." , ".$a[$i]; } } } } else echo "q is empty!<br>"; //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; echo " to "; echo $q; echo "<br>"; ?>