php+ajax 实现输入读取数据库显示匹配信息

前端之家收集整理的这篇文章主要介绍了php+ajax 实现输入读取数据库显示匹配信息前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

废话不多说了,直接跟大家贴代码

dropBox_index.PHP

PHP;"> 主<a href="/tag/sousuo/" target="_blank" class="keywords">搜索</a>引擎下拉框<a href="/tag/zidong/" target="_blank" class="keywords">自动</a><a href="/tag/xianshi/" target="_blank" class="keywords">显示</a>数据 Box.css" type="text/css" rel="stylesheet"/>

Box">

dropBox.js ajax

PHP;"> 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;"> 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);
$<a href="/tag/sql/" target="_blank" class="keywords">sql</a> = "select * from persons";
//$<a href="/tag/sql/" target="_blank" class="keywords">sql</a>="SELECT FirstName FROM Persons where Firstname like '%$q%'";

$result =<a href="/tag/MysqL/" target="_blank" class="keywords">MysqL</a>_query($<a href="/tag/sql/" target="_blank" class="keywords">sql</a>,$con);
while($row = <a href="/tag/MysqL/" target="_blank" class="keywords">MysqL</a>_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 />";
 }
<a href="/tag/MysqL/" target="_blank" class="keywords">MysqL</a>_close($con);

}
?>

数据库

以上就是本文给大家介绍的PHP+ajax 实现输入读取数据库显示匹配信息的全部内容,希望大家喜欢。

猜你在找的PHP相关文章