AJAX can be used for interactive communication with a database.
The Below example will demonstrate how a web page can fetch information from a database with AJAX:
The database table we use in the example above looks like this:
When a user selects a user in the dropdown list above, a function called "showUser()" is executed. The function is triggered by the "onchange" event:
The showUser() function does the following:
The page on the server called by the JavaScript above is a PHP file called "getuser.html".
The source code in "getuser.html" runs a query against a MySQL database, and returns the result in an HTML table:
Explanation: When the query is sent from the JavaScript to the PHP file, the following happens:
Your Query was successfully sent!