WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

PHP - AJAX and MySQL


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:

Example


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:

Example

<html>
<head>
<script>
function showUser(str) {
    if (str == "") {
        document.getElementById("txtHint").innerHTML = "";
        return;
    } 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;
            }
        };
        xmlhttp.open("GET","getuser.php?q="+str,true);
        xmlhttp.send();
    }
}
</script>
</head>
<body>

<form>
<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>
  </select>
</form>
<br>
<div id="txtHint"><b>Person data will be recorded here...</b></div>

</body>
</html>

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>
<html>
<head>
<style>
table {
    width: 100%;
    outskirt breakdown: collapse;
}

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

th {text-adjust: left;}
</style>
</head>
<body>

<?php
$q = intval($_GET['q']);

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

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

reverberation "<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";
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>";
mysqli_close($con);
?>
</body>
</html>

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