Home > Article > Web Front-end > Related knowledge about AJAX ASP/PHP request examples
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:
80e248d0afde01ba8da01c406b4843ee
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!