AJAX use for intelligent correspondence with a database.

AJAX Database Example 1

The following precedent a website page can get data from a database with AJAX:


Person information will be recorded here...

Example Explained - The MySQL Database

The database table we use in the precedent above looks like this:

id FirstName LastName Age Hometown Job
1 Rajesh morise 31 India Teacher
2 Lois morisen 45 USA Teacher

Example Explained

In the precedent above, when a client chooses an individual in the dropdown list over, a capacity called "showUser()" is executed.

The work is activated by the onchange event.

Here is the HTML code:


function showUser(str) {
    if (str == "") {
        document.getElementById("txtHint").innerHTML = "";
    } else {
        if (window.XMLHttpRequest) {
           /code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else {
           / code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        xmlhttp.onreadystatechange = work() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("txtHint").innerHTML = xmlhttp.responseText;

<select name="users" onchange="showUser(this.value)">
  <option value="">Select a person:</option>
  <option value="1">Peter Griffin</option>
  <option value="2"> </option>
  <option value="3">Joseph Swanson</option>
  <option value="4">Glenn Quagmire</option>
<div id="txtHint"><b>Person data will be recorded here...</b></div>


Code explanation:

First, check if no individual is chosen (str == ""). On the off chance that no individual is chosen, clear the substance of the txtHint placeholder and leave the function.

If an individual is chosen, do the following:

  • Create a XMLHttpRequest object
  • Create the capacity to be executed when the server reaction is ready
  • Send the demand off to a document on the server
  • Notice that a parameter (q) is added to the URL (with the substance of the dropdown list)

The PHP File

The page on the server called by the JavaScript above is a PHP document called "getuser.php".

The source code in "getuser.php" runs a question against a MySQL database, and returns the outcome in a HTML table:

<!DOCTYPE html>
table {
    width: 100%;
    outskirt breakdown: collapse;

table, td, th {
    outskirt: 1px strong black;
    cushioning: 5px;

th {text-adjust: left;}

$q = intval($_GET['q']);

$con = mysqli_connect('localhost','peter','abc123','my_db');
in the event that (!$con) {
    die('Could not interface: ' . mysqli_error($con));

$sql="SELECT * FROM client WHERE id = '".$q."'";
$result = mysqli_query($con,$sql);

reverberation "<table>
while($row = mysqli_fetch_array($result)) {
    reverberation "<tr>";
    echo "<td>" . $row['FirstName'] . "</td>";
    reverberation "<td>" . $row['LastName'] . "</td>";
    echo "<td>" . $row['Age'] . "</td>";
    reverberation "<td>" . $row['Hometown'] . "</td>";
    echo "<td>" . $row['Job'] . "</td>";
    reverberation "</tr>";
reverberation "</table>";

Explanation: When the question is sent from the JavaScript to the PHP document, the accompanying happens:

  1. PHP opens an association with a MySQL server
  2. The right individual is found
  3. An HTML table is made, loaded up with information, and sent back to the "txtHint" placeholder