php隨筆2-php+ajax 實現輸入讀取資料庫顯示匹配資訊

來源:互聯網
上載者:User

標籤:

dropbox_index.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>主搜尋引擎下拉框自動顯示資料</title></head><script src="js/dropbox.js"  type="text/javascript"></script><link  href="css/dropbox.css" type="text/css" rel="stylesheet"/><body><form action="" method="post" enctype="multipart/form-data">                <input name="txt" id="txt" class="txt" type="text" onkeyup ="showHint(this.value)"  />                        <input type="submit" class="btn" name="submit" value="Upload" /><br />                <span id="txtHint" class="file-box"></span></form></body></html>

dropbox.js    ajax

        var xmlHttp                        function showHint(str)        {            if (str.length==0)            {               document.getElementById("txtHint").innerHTML=""              return            }            xmlHttp=GetXmlHttpObject()            if (xmlHttp==null)              {                  alert ("Browser does not support HTTP Request")                  return              }             var url="responsepage.php"            url=url+"?q="+str            url=url+"&sid="+Math.random()            xmlHttp.onreadystatechange=stateChanged             xmlHttp.open("GET",url,true)            xmlHttp.send(null)        }                         //設定回呼函數        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;        }

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"];    //全部小寫化    $q=strtolower($q);        //非空驗證    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))         {                           //匹配判斷              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);     }?>

資料庫:

 結果:

php隨筆2-php+ajax 實現輸入讀取資料庫顯示匹配資訊

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.