AJAX Database Example

AJAX can be used for interactive communication with a database.

The following example will demonstrate how a web page can fetch information from a database with AJAX:

The showCustomer() function does the following:

Check if a customer is selected

Create an XMLHttpRequest object

Create the function to be executed when the server response is ready

Send the request off to a file on the server

Notice that a parameter (q) is added to the URL (with the content of the dropdown list)

EXAMPLE ❯


The AJAX Server Page

The page on the server called by the JavaScript above is a PHP file called "getcustomer.php".

The source code in "getcustomer.php" runs a query against a database, and returns the result in an HTML table:

EXAMPLE ❯