在下面的 AJAX 例子中,我們將演示當(dāng)用戶向 web 表單中輸入數(shù)據(jù)時(shí),網(wǎng)頁(yè)如何與在線的 web 服務(wù)器進(jìn)行通信。
Suggestions:
這個(gè)例子包括三張頁(yè)面:
這是 HTML 表單。它包含一個(gè)簡(jiǎn)單的 HTML 表單和指向 JavaScript 的鏈接:
<html> <head> <script src="clienthint.js"></script> </head> <body> <form> First Name: <input type="text" id="txt1" onkeyup="showHint(this.value)"> </form> <p>Suggestions: <span id="txtHint"></span></p> </body> </html>
正如您看到的,上面的 HTML 頁(yè)面含有一個(gè)簡(jiǎn)單的 HTML 表單,其中帶有一個(gè)名為 "txt1" 的輸入字段。
該表單是這樣工作的:
JavaScript 代碼存儲(chǔ)在 "clienthint.js" 文件中,它被鏈接到 HTML 文檔:
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="gethint.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; }
每當(dāng)在輸入域中輸入一個(gè)字符,該函數(shù)就會(huì)被執(zhí)行一次。
如果文本框中有內(nèi)容 (str.length > 0),該函數(shù)這樣執(zhí)行:
如果輸入域?yàn)榭?,則函數(shù)簡(jiǎn)單地清空 txtHint 占位符的內(nèi)容。
每當(dāng) XMLHTTP 對(duì)象的狀態(tài)發(fā)生改變,則執(zhí)行該函數(shù)。
在狀態(tài)變成 4 (或 "complete")時(shí),用響應(yīng)文本填充 txtHint 占位符 txtHint 的內(nèi)容。
AJAX 應(yīng)用程序只能運(yùn)行在完整支持 XML 的 web 瀏覽器中。
上面的代碼調(diào)用了名為 GetXmlHttpObject() 的函數(shù)。
該函數(shù)的作用是解決為不同瀏覽器創(chuàng)建不同 XMLHTTP 對(duì)象的問(wèn)題。
這一點(diǎn)在上一節(jié)中已經(jīng)解釋過(guò)了。
被 JavaScript 代碼調(diào)用的服務(wù)器頁(yè)面是一個(gè)名為 "gethint.php" 的簡(jiǎn)單服務(wù)器頁(yè)面。
"gethint.php" 中的代碼會(huì)檢查名字?jǐn)?shù)組,然后向客戶端返回對(duì)應(yīng)的名字:
<?php // Fill up array with names $a[]="Anna"; $a[]="Brittany"; $a[]="Cinderella"; $a[]="Diana"; $a[]="Eva"; $a[]="Fiona"; $a[]="Gunda"; $a[]="Hege"; $a[]="Inga"; $a[]="Johanna"; $a[]="Kitty"; $a[]="Linda"; $a[]="Nina"; $a[]="Ophelia"; $a[]="Petunia"; $a[]="Amanda"; $a[]="Raquel"; $a[]="Cindy"; $a[]="Doris"; $a[]="Eve"; $a[]="Evita"; $a[]="Sunniva"; $a[]="Tove"; $a[]="Unni"; $a[]="Violet"; $a[]="Liza"; $a[]="Elizabeth"; $a[]="Ellen"; $a[]="Wenche"; $a[]="Vicky"; //get the q parameter from URL $q=$_GET["q"]; //lookup all hints from array if length of q>0 if (strlen($q) > 0) { $hint=""; for($i=0; $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 ($hint == "") { $response="no suggestion"; } else { $response=$hint; } //output the response echo $response; ?>
如果存在從 JavaScript 送來(lái)的文本 (strlen($q) > 0),則:
如對(duì)本文有疑問(wèn),請(qǐng)?zhí)峤坏浇涣髡搲?,廣大熱心網(wǎng)友會(huì)為你解答!! 點(diǎn)擊進(jìn)入論壇
Powered by 365建站網(wǎng) RSS地圖 HTML地圖
copyright © 2013-2024 版權(quán)所有 鄂ICP備17013400號(hào)