dropbox_index.php
<!DOCTYPE HTML Public "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/TR/xhtml1/DTD/ Xhtml1-transitional.dtd "><HTMLxmlns= "http://www.w3.org/1999/xhtml"><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=gb2312" /><title>The main search engine drop-down box automatically displays the data</title></Head><Scriptsrc= "Js/dropbox.js"type= "Text/javascript"></Script><Linkhref= "Css/dropbox.css"type= "Text/css"rel= "stylesheet"/><Body><formAction=""Method= "POST"enctype= "Multipart/form-data"> <inputname= "txt"ID= "txt"class= "txt"type= "text"onkeyup= "Showhint (this.value)" /> <inputtype= "Submit"class= "BTN"name= "Submit"value= "Upload" /><BR/> <spanID= "Txthint"class= "File-box"></span></form></Body></HTML>
Dropbox.js Ajax
varxmlHttpfunctionShowhint (str) {if(str.length==0) {document.getElementById ("Txthint"). Innerhtml= ""return} xmlHttp=Getxmlhttpobject ()if(xmlhttp==NULL) {alert ("Browser does not support HTTP Request") return } varUrl= "responsepage.php"URL=url+ "? q=" +str URL=url+ "&sid=" +math.random () Xmlhttp.onreadystatechange=statechanged Xmlhttp.open ("GET", URL,true) Xmlhttp.send (NULL) } //Setting the callback function functionstatechanged () {if(xmlhttp.readystate==4 | | xmlhttp.readystate== "COMPLETE") {document.getElementById ("Txthint"). innerhtml=Xmlhttp.responsetext}}functionGetxmlhttpobject () {varxmlhttp=NULL; Try { //Firefox, Opera 8.0+, Safarixmlhttp=NewXMLHttpRequest (); } Catch(e) {//Internet Explorer Try{xmlHttp=NewActiveXObject ("Msxml2.xmlhttp"); } Catch(e) {xmlHttp=NewActiveXObject ("Microsoft.XMLHTTP"); } } returnxmlHttp; }
Dropbox.css
. txt { height:22px; border:1px solid #cdcdcd; width:220px; border-right:none;} . BTN { Background-color:#FFF; border:1px solid #CDCDCD; height:26px; width:70px;}
responsepage.php
<?PHP//get the Q parameter from URL $q=$_get["Q"]; //All lowercase $q=Strtolower($q); //non-null validation if(isset($q) &&$q! = ") { $con=mysql_connect("localhost", "root", "Lifu"); if(!$con) { die(' Could not connect: '.Mysql_error()); } mysql_select_db("my_db",$con); $sql= "SELECT * FROM persons"; //$sql = "Select FirstName from Persons where FirstName like '% $q% '"; $result=mysql_query($sql,$con); while($row=Mysql_fetch_array($result)) { //Match judgment if(Stristr(Strtolower($row[' FirstName ']),$q)) { //echo "-----------------Persons-----------------"; Echo /*"FirstName:".*/ $row[' FirstName ']. "<br/>"; //echo "LastName:". $row [' LastName ']. "<br/>"; echo "Age:". $row [' Age ']. "<br/>"; } //echo $row [' FirstName ']. "<br/>"; } Mysql_close($con); }?>
Database:
Results:
PHP essay 2-php+ajax Implementation input read database display matching information