PHP essay 2-php+ajax Implementation input read database display matching information

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.