AJAX is used to create more interactive applications.
The Below example will demonstrate how a web page can communicate with a web server while a user type characters in an input field:
Start typing a name in the input field below:
Suggestions:
When a user types a character in the input field above, the function "showHint()" is executed. The function is triggered by the "onkeyup" event:
Source code explanation:
If the input field is empty (str.length==0), the function clears Content of the txtHint placeholder and exits the function.
If the input field is not empty, the showHint() function executes the following:
The page on the server called by the JavaScript above is a PHP file called "gethint.html".
The source code in "gethint.html" checks an array of names, and returns the corresponding name(s) to the browser:
Explanation: If there is any text sent from the JavaScript (strlen($q) > 0), the following happens:
Your Query was successfully sent!