PHP - AJAX vs. PHP
PHP - AJAX with PHP
AJAX is used to create more interactive applications.
AJAX PHP Example
The following example will demonstrate how the web page communicates with the Web server when the user types characters in the input box:
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:
在输入框中输入一个姓名:
返回值:
Source code explanation:
If the input box is empty (str.length==0), this function will clear the txtHint placeholder content 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
·
# ·PHP 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 PHP Ajax cross-domain problem solution If your asynchronous request needs to cross domain, you can check out: PHP Ajax cross-domain problem solution.