PHP ?? ?? ???? AJAX ? PHP
AJAX ? PHP ??
? ?? ??: AJAX? ?? ??? ??????? ??? ? ?????
?? ?? ???? ?? ??? ??? ??? ? ? ???? ? ??? ??? ????? ?????.
??? ??? ??? ??? ?????
?? ?? - HTML ???
? ???? ???? ??? ???? "showHint()" ??? ?????. ? ??? "onkeyup" ???? ?? ?????:
??: onkeyup ???? ??? ?? ?? ? ?????.
???? ??? ??? ????
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script> function showHint(str){ //如果輸入框是空的(str.length==0),該函數(shù)會(huì)清空 txtHint 占位符的內(nèi)容,并退出該函數(shù)。 if(str.length==0){ document.getElementById("txtHint").innerHTML=""; return; } //如果輸入框不是空的,那么 showHint() 會(huì)執(zhí)行以下代碼: //創(chuàng)建XMLHttpRequest對象 if(window.XMLHttpRequest){ //IE7,Firefox,Chrome,Opera,Safari 瀏覽器執(zhí)行的代碼 xmlhttp=new XMLHttpRequest(); }else{ //IE6,IE5瀏覽器執(zhí)行的代碼 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } //在服務(wù)器響應(yīng)的時(shí)候執(zhí)行的函數(shù) xmlhttp.onreadystatechange=function(){ if(xmlhttp.readyState==4&&xmlhttp.status==200){ document.getElementById("txtHint").innerHTML=xmlhttp.responseText; } } //向服務(wù)器上的文件發(fā)送請求 xmlhttp.open("GET","2.php?q="+str,true); xmlhttp.send(); } </script> </head> <body> <p><b>在輸入框中輸入一個(gè)姓名</b></p> <form> <!-- onkeyup 事件會(huì)在鍵盤按鍵被松開時(shí)發(fā)生,鍵盤松開時(shí),調(diào)用showHint()函數(shù)--> 姓名:<input type="text" onkeyup="showHint(this.value)"> <p>返回值:<span id="txtHint"></span></p> </form> </body> </html>
?? ?? ?? :
?? ??? ?? ?? ??(str.length==0) ? ??? txtHint ?? ???? ??? ??? ??? ?????.
?? ??? ?? ?? ??? showHint()? ?? ??? ?????.
XMLHttpRequest ?? ??
?? ?? ? ??? ?? ?? ???
??? ??? ?? ???
URL ?? ??? ????(q)? ?????(?? ??? ?? ??)
?? ?? - PHP ??
??? JavaScript? ?? ???? ?? ???? "2.php"?? ??? PHP ?????.
"2.php"? ?? ??? ?? ??? ??? ? ?? ??? ????? ?????. ??? ??? ????.
<?php // 將姓名填充到數(shù)組中 $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"; $a[]="小明"; //從請求URL地址中獲取 q 參數(shù) $q=$_GET["q"]; //查找是否由匹配值, 如果 q>0 if (strlen($q) > 0) { $hint=""; for($i=0; $i<count($a); $i++) { //將$a數(shù)組和$q全部轉(zhuǎn)換為小寫,然后逐個(gè)取出$a,截取與$q相同長度,比較是否相同,相同放入$hint中 if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q)))) { if ($hint=="") { $hint=$a[$i]; } else { $hint=$hint." , ".$a[$i]; } } } } // 如果沒有匹配值設(shè)置輸出為 "no suggestion" if ($hint == "") { $response="no suggestion"; } else { $response=$hint; } //輸出返回值 echo $response; ?>
??:
JavaScript? ???(?: strlen($q) > 0)? ??? ??? ?? ?? ?????.
JavaScript?? ?? ??? ???? ??? ????.
??? ???? ??? ???? ?? ???? "?? ??"?? ??
???? ??? ?? ?? ???? ?? ???? ?? ???? ?????.
??? "txtHint"? ????. " ?? ???
?? ??
? ???? ?? ?? ?? ???? ???? ????.
? ??
Onkeyup ???: ??? ?? ??? ? ??
?? ??, ?? ? ??
AJAX XMLHttpRequest ?? ??, ?? ?? ? ???? ??, ? ?? ????? ?? ?? ??: ?? ??? 1-5 ??
HTML DOM getElementById() ???: ??? ID? ?? ? ?? ??? ?? ??? ?????
?? ?? ??
Get ?? ?? ??
??? ?? ??:
strlen()
count()
Strtolower()