ASP - AJAX with ASP
AJAX is used to create more interactive applications.
AJAX ASP Example
The following example will demonstrate how a web page can communicate with a web server while a user types characters in an input field:
Example
Start typing a name in the input field below:
Suggestions:
Example Explanation - HTML Page
When a user types a character in the input field above, the "showHint()" function is executed. This function is triggered by the "onkeyup" event:
Source Code Explanation:
If the input field is empty (str.length==0), the function clears the content of the txtHint placeholder and exits the function.
If the input field is not empty, then showHint() performs the following steps:
Creates an XMLHttpRequest object
Creates a function that is executed when the server response is ready
Sends a request to a file on the server
Note the parameter (q) added to the URL's end (containing the content of the input field)
ASP File
The server page called by the JavaScript above is an ASP file named "gethint.asp".
The source code in "gethint.asp" checks an array of names and returns the corresponding names to the browser:
Explanation: If JavaScript sends any text (i.e., strlen($q) > 0), then:
Searches for names matching the characters sent by JavaScript
If no matches are found, sets the response string to "no suggestion"
If one or more matching names are found, sets the response string with all the names
Sends the response to the "txtHint" placeholder