AJAX is used to create more dynamic applications. This article will explain its related knowledge.
Explanation of examples - showHint() function
When the user types characters in the input box above, the function "showHint()" will be executed. This function is triggered by the "onkeyup" event:
function showHint(str) { var xmlhttp; if (str.length==0) { document.getElementById("txtHint").innerHTML=""; return; } if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("txtHint").innerHTML=xmlhttp.responseText; } } xmlhttp.open("GET","gethint.asp?q="+str,true); xmlhttp.send(); }
Source code explanation:
If the input box is empty (str.length==0), then this function is cleared txtHint The content of the placeholder and exits the function.
If the input box is not empty, the showHint() function performs the following tasks:
Creates an XMLHttpRequest object
Execute the function when the server response is ready
The request is sent to a file on the server
Please note that we added a parameter q (with the content of the input box) to the URL
AJAX Server Page - ASP and PHP
The server page called by the above JavaScript is an ASP file named "gethint.asp".
Below, we create two versions of the server file, one written in ASP and the other written in PHP.
ASP file
The source code in "gethint.asp" checks an array of names and returns the corresponding names to the browser:
<%
response.expires=-1
dim a(30)'Fill the array a(1)="Anna"
a(2)="Brittany" a(3)="Cinderella" a(4)="Diana" a(5)="Eva" a(6)="Fiona" a(7)="Gunda" a(8)="Hege" a(9)="Inga" a(10)="Johanna" a(11)="Kitty" a(12)="Linda" a(13)="Nina" a(14)="Ophelia" a(15)="Petunia" a(16)="Amanda" a(17)="Raquel" a(18)="Cindy" a(19)="Doris" a(20)="Eve" a(21)="Evita" a(22)="Sunniva" a(23)="Tove" a(24)="Unni" a(25)="Violet" a(26)="Liza" a(27)="Elizabeth" a(28)="Ellen" a(29)="Wenche" a(30)="Vicky"'获得来自 URL 的 q 参数q=ucase(request.querystring("q"))'如果 q 大于 0,则查找数组中的所有提示if len(q)>0 then hint="" for i=1 to 30 if q=ucase(mid(a(i),1,len(q))) then if hint="" then hint=a(i) else hint=hint & " , " & a(i) end if end if next
end with names if'If the prompt is not found, output "no suggestion"'otherwise output the correct value if hint="" then
response.write("no suggestion")
else
response.write(hint)
end if
%>
PHP file
The following code is written in PHP and has the same function as the above ASP code.
Note: To run this example in PHP, please change the value of the url variable (in the Javascript code) from "gethint.asp" to "gethint.php".
$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";//获得来自 URL 的 q 参数$q=$_GET["q"];//如果 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]; } } } }// 如果未找到提示,则把输出设置为 "no suggestion"// 否则设置为正确的值if ($hint == "") { $response="no suggestion"; } else { $response=$hint; }//输出响应echo $response; ?>
This article explains the AJAX ASP/PHP request , for more learning materials, please pay attention to the php Chinese website to view.
Related recommendations:
Explanation of XML in AJAX with examples
MySQL’s first introduction to sql statements and library operations
How to simply implement it through TP5-add, delete, modify and check
The above is the detailed content of Related knowledge about AJAX ASP/PHP request examples. For more information, please follow other related articles on the PHP Chinese website!