本文實(shí)例講述了PHP+JS實(shí)現(xiàn)的實(shí)時(shí)搜索提示功能。分享給大家供大家參考,具體如下:
效果圖如下:

代碼如下:
HTML代碼:(該代碼用兩種方法實(shí)現(xiàn),一種Jquery,一種原生JS)
html>
head>
script src="/DelphiRequest/search/js/jquery.js">/script>
script>
/*用原生js實(shí)現(xiàn)
// function showResult(str)
// {
// if (str.length==0)
// {
// document.getElementById("livesearch").innerHTML="";
// document.getElementById("livesearch").style.border="0px";
// return;
// }
// if (window.XMLHttpRequest)
// {// IE7+, Firefox, Chrome, Opera, Safari 瀏覽器執(zhí)行
// xmlhttp=new XMLHttpRequest();
// }
// else
// {// IE6, IE5 瀏覽器執(zhí)行
// xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
// }
// xmlhttp.onreadystatechange=function()
// {
// if (xmlhttp.readyState==4 xmlhttp.status==200)
// {
// document.getElementById("livesearch").innerHTML=xmlhttp.responseText;
// document.getElementById("livesearch").style.border="1px solid #A5ACB2";
// }
// }
// xmlhttp.open("GET","livesearch.php?q="+str,true);
// xmlhttp.send();
// }
*/
//用jquery實(shí)現(xiàn)
function showResult(str){
$.ajax({
type: "GET",
url : "livesearch.php",
datatype : 'json',
data: {'q':str} ,
success :function (data) {
document.getElementById("livesearch").innerHTML=data;
document.getElementById("livesearch").style.border="1px solid #A5ACB2";
}
})
}
/script>
/head>
body>
form>
input type="text" size="30" onkeyup="showResult(this.value)">
div id="livesearch">/div>
/form>
/body>
/html>
PHP代碼如下:(PHP不僅可以考慮直接使用數(shù)組,也可以考慮直接查詢數(shù)據(jù)庫,獲取數(shù)據(jù)庫內(nèi)容,本代碼使用的是數(shù)組。)
?php
$provinces=array("beijing","tianjin","shanghai","chongqing","hebei","henan","heilongjiang","jilin","changchun",
"shandong","anhui","shanxi","guangzhou","yunnan","hainan","xizang","qinghai","fujian","guizhou","jiangsu",
"zhejiang","guangzhou","yunan","hainan","xizang","neimenggu","sichuan","gansu","ningxia","xianggang","aomen");
$tmp=$_GET['q'];
$val=array();
$k=0;
if (strlen($tmp)>0)
{
for($i=0;$i31;$i++){
if(strpos($provinces[$i],$tmp)!==false){
//傳遞值給val
$val[$k]=$provinces[$i];
//下標(biāo)增加
$k=$k+1;
}
}
//遍歷val數(shù)組
for($j=0;$jcount($val);$j++)
{
echo $val[$j];
echo "br>";
}
}
?>
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》、《php程序設(shè)計(jì)算法總結(jié)》、《PHP+ajax技巧與應(yīng)用小結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《php字符串(string)用法總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》及《PHP常用遍歷算法與技巧總結(jié)》
希望本文所述對大家PHP程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:- php+ajax做仿百度搜索下拉自動提示框(有實(shí)例)
- php 搜索框提示(自動完成)實(shí)例代碼
- jquery+php實(shí)現(xiàn)搜索框自動提示
- 利用PHP+JS實(shí)現(xiàn)搜索自動提示(實(shí)例)
- 谷歌音樂搜索欄的提示功能php修正代碼
- PHP 搜索查詢功能實(shí)現(xiàn)
- php站內(nèi)搜索并高亮顯示關(guān)鍵字的實(shí)現(xiàn)代碼
- PHP獲取搜索引擎關(guān)鍵字來源的函數(shù)(支持百度和谷歌等搜索引擎)
- PHP寫的獲取各搜索蜘蛛爬行記錄代碼
- php使用正則表達(dá)式進(jìn)行字符串搜索的方法