日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

php的ajax实例

發布時間:2025/1/21 php 135 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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实例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。