AJAX Database Example
AJAX can be used to interact with databases for interactive communication.
AJAX Database Example
The following example will demonstrate how a web page can retrieve information from a database using AJAX:
Example
Example Explanation - HTML Page
When a user selects a customer from the dropdown list above, a function called "showCustomer()" is executed. This function is triggered by the "onchange" event:
Source Code Explanation:
If no customer is selected (str.length == 0), the function will clear the txtHint placeholder and then exit.
If a customer is selected, the showCustomer() function will perform the following steps:
Create an XMLHttpRequest object
Create a function that will be executed when the server response is ready
Send a request to a file on the server
Note the parameter (q) added to the URL末端 (containing the content of the dropdown list)
ASP File
The server page called by the JavaScript above is an ASP file named "getcustomer.asp".
The source code in "getcustomer.asp" will run a query against the database and return the results in an HTML table: