html>
head>
script src="/DelphiRequest/search/js/jquery.js">/script>
script>
/*用原生js實現
// 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 瀏覽器執行
// xmlhttp=new XMLHttpRequest();
// }
// else
// {// IE6, IE5 瀏覽器執行
// 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實現
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
$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];
//下標增加
$k=$k+1;
}
}
//遍歷val數組
for($j=0;$jcount($val);$j++)
{
echo $val[$j];
echo "br>";
}
}
?>
更多關于PHP相關內容感興趣的讀者可查看本站專題:《PHP數據結構與算法教程》、《php程序設計算法總結》、《PHP+ajax技巧與應用小結》、《PHP網絡編程技巧總結》、《php字符串(string)用法總結》、《PHP數組(Array)操作技巧大全》及《PHP常用遍歷算法與技巧總結》