AJAX and PHP
Explanation of examples - HTML page
When the user types characters in the input box above, the "showHint()" function will be executed. This function is triggered by the "onkeyup" event:
Title 在输入框中输入一个姓名:
返回值:
Source code explanation:
If the input box is empty (str.length== 0), this function will clear the contents of the txtHint placeholder and exit the function.
If the input box is not empty, then showHint() will perform the following steps:
· Create an XMLHttpRequest object
· Create a function to be executed when the server response is ready
· # File
The server page called above through JavaScript is a PHP file named "gethint.php".The source code in "gethint.php" checks the name array and returns the corresponding name to the browser:
0 if (strlen($q) > 0) { $hint=""; for($i=0; $iExplanation: If JavaScript sends any text (i.e. strlen($q ) > 0), then:
1. Find names matching the characters sent by JavaScript
2. If no match is found, set the response string to "no suggestion"
3. If one or more matching names are found, set the response string with all names
4. Send the response to the "txtHint" placeholder