php的ajax实例
當輸入j后,會觸發ajax效果,從后臺獲取相應的名字中帶有j的數據,并展示在suggestions中。
代碼實現如下:
實現ajax需要三個文件,一個是html的表單文件,一個是js的核心文件,一個是php的后臺文件。
下面的是html文件,當鍵盤按下時觸發showHint方法,在showHint方法中會有ajax的核心內容,實例化,獲取地址,獲取數據并展示等等。
<html>
<head>
<script src="clienthint.js"></script>?
</head>
<body>
<form>?
First Name:
<input type="text" id="txt1"
οnkeyup="showHint(this.value)">
</form>
<p>Suggestions: <span id="txtHint"></span></p>
</body>
</html>
?
下面是js的內容clienthint.js。
?
var xmlHttp
function showHint(str)
{
if (str.length==0)
?{?
?document.getElementById("txtHint").innerHTML=""
?return
?}
//獲取xmlHttpObject對象,如果為空,提示瀏覽器不支持ajax
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
?{
?alert ("Browser does not support HTTP Request")
?return
?}?
?//獲取url
var url="gethint.php"
url=url+"?q="+str
url=url+"&sid="+Math.random()
?//回調函數,執行動作
xmlHttp.onreadystatechange=stateChanged?
?//open
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}?
function stateChanged()?
{?
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{?
//將獲取的信息插入到txtHint中
document.getElementById("txtHint").innerHTML=xmlHttp.responseText?
}?
}
//獲取xml對象
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;
}
?
下面是php的內容。根據ajax對象傳入的參數,獲取相應的數據。
<?php
// Fill up array with names
a[]="Anna";a[]="Anna";a[]="Brittany";
a[]="Cinderella";a[]="Cinderella";a[]="Diana";
a[]="Eva";a[]="Eva";a[]="Fiona";
a[]="Gunda";a[]="Gunda";a[]="Hege";
a[]="Inga";a[]="Inga";a[]="Johanna";
a[]="Jiqing";a[]="Jiqing";a[]="Kitty";
a[]="Linda";a[]="Linda";a[]="Nina";
a[]="Ophelia";a[]="Ophelia";a[]="Petunia";
a[]="Amanda";a[]="Amanda";a[]="Raquel";
a[]="Cindy";a[]="Cindy";a[]="Doris";
a[]="Eve";a[]="Eve";a[]="Evita";
a[]="Sunniva";a[]="Sunniva";a[]="Tove";
a[]="Unni";a[]="Unni";a[]="Violet";
a[]="Liza";a[]="Liza";a[]="Elizabeth";
a[]="Ellen";a[]="Ellen";a[]="Wenche";
a[]="Vicky";//gettheqparameterfromURLa[]="Vicky";//gettheqparameterfromURLq=GET["q"];//lookupallhintsfromarrayiflengthofq>0if(strlen(GET["q"];//lookupallhintsfromarrayiflengthofq>0if(strlen(q) > 0)
{
hint="";for(hint="";for(i=0;?i<count(i<count(a);?i++)???{???if?(strtolower($q)==strtolower(substr($a[$i],0,strlen($q))))?????{?????if?($hint=="")???????{???????$hint=$a[$i];???????}?????else???????{???????$hint=$hint."?,?".$a[$i];???????}?????}???}?}??//Set?output?to?"no?suggestion"?if?no?hint?were?found?//or?to?the?correct?values?if?(i++)???{???if?(strtolower($q)==strtolower(substr($a[$i],0,strlen($q))))?????{?????if?($hint=="")???????{???????$hint=$a[$i];???????}?????else???????{???????$hint=$hint."?,?".$a[$i];???????}?????}???}?}??//Set?output?to?"no?suggestion"?if?no?hint?were?found?//or?to?the?correct?values?if?(hint == "")
{
response="no?suggestion";?}?else?{?$response=$hint;?}??//output?the?response?echoresponse="no?suggestion";?}?else?{?$response=$hint;?}??//output?the?response?echoresponse;
?>
本文轉自TBHacker博客園博客,原文鏈接:http://www.cnblogs.com/jiqing9006/archive/2012/06/27/2565763.html,如需轉載請自行聯系原作者
與50位技術專家面對面20年技術見證,附贈技術全景圖
總結
以上是生活随笔為你收集整理的php的ajax实例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: markdown grammar
- 下一篇: 最多包含2/k个不同字符的最长串