PHP and AJAX MySQL Database Example
- Vorige pagina AJAX XML
- Volgende pagina AJAX responseXML
AJAX can be used for interactive communication with databases.
AJAX Database Example
In the following AJAX example, we will demonstrate how a web page uses AJAX technology to read information from a MySQL database.
Select a customer from the dropdown list
This application consists of four elements:
- MySQL Database
- A simple HTML form
- JavaScript
- PHP Page
Database
The database used in this example looks something like this:
id | CustomerID | CompanyName | ContactName | Address | City | PostalCode | Country |
---|---|---|---|---|---|---|---|
1 | Alibaba | Alibaba Group | Ma Yun | ...... | Hangzhou | 310002 | China |
2 | APPLE | Apple Inc. | Tim Cook | ...... | Cupertino | 95014 | USA |
3 | BAIDU | BAIDU Inc. | Li YanHong | ...... | Beijing | 100080 | China |
4 | Canon | Canon USA Inc. | Tsuneji Uchida | ...... | New York | 11042 | USA |
Example Explanation
In the above example, when the user selects a customer from the dropdown list above, the function named "showUser()" is executed.
This function is triggered by the onchange event.
This is the HTML code:
Example
<!doctype html> <html> <head> <script> function showUser(str) { if (str=="") { document.getElementById("txtHint").innerHTML=""; return; } 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=function() { if (this.readyState==4 && this.status==200) { document.getElementById("txtHint").innerHTML=this.responseText; } } xmlhttp.open("GET","/demo/getcustomer.php?q="+str,true); xmlhttp.send(); } </script> </head> <body> <form> <select name="users" onchange="showUser(this.value)"> <option value="">Select a customer:</option> <option value="1">Alibaba</option> <option value="2">APPLE</option> <option value="3">BAIDU</option> <option value="4">Canon</option> <option value="5">Google</option> <option value="6">HUAWEI</option> <option value="7">Microsoft</option> <option value="8">Nokia</option> <option value="9">SONY</option> <option value="10">Tencent</option> </select> </form> <br> <div id="txtHint"><b>De klantinformatie wordt hier weergegeven.</b></div> </body> </html>
Code-exploratie:
Eerst, controleer of een klant is gekozen. Als geen klant is gekozen (str == ""), verwijder de inhoud van txtHint en verlaat de functie. Als een klant is gekozen, voer dan de volgende stappen uit:
- Maak een XMLHttpRequest-object aan
- Maak een functie aan die moet worden uitgevoerd wanneer de serverresponse klaar is
- Verstuur de verzoek naar het bestand op de server
- Let op, de parameter (q) wordt toegevoegd aan de URL (met inhoud van een keuzelijst)
PHP-bestand
De serverpagina die wordt aangeroepen door JavaScript is een PHP-bestand genaamd "getuser.php".
De broncode in "getuser.php" voert een query uit op de MySQL-database en toont de resultaten in een HTML-tabel:
<?php $q = intval($_GET['q']); $con = mysqli_connect('MyServer','MyUser','MyPassword','Customers'); if (!$con) { die('Could not connect: ' . mysqli_error($con)); } $sql="SELECT * FROM customerslist WHERE id = '".$q."'"; $result = mysqli_query($con,$sql); echo "<table>"; while($row = mysqli_fetch_array($result)) { echo "<tr><th>CustomerID</th><td>" . $row['CustomerID'] . "</td></tr>"; echo "<tr><th>Bedrijfsnaam</th><td>" . $row['CompanyName'] . "</td></tr>"; echo "<tr><th>Contactpersoon</th><td>" . $row['ContactName'] . "</td></tr>"; echo "<tr><th>Adres</th><td>" . $row['Address'] . "</td></tr>"; echo "<tr><th>Stad</th><td>" . $row['City'] . "</td></tr>"; echo "<tr><th>Postcode</th><td>" . $row['PostalCode'] . "</td></tr>"; echo "<tr><th>Land</th><td>" . $row['Country'] . "</td></tr>"; } echo "</table>"; mysqli_close($con); ?>
Uitleg: Wanneer een query van JavaScript naar een PHP-bestand wordt gestuurd, vindt het volgende plaats:
- PHP verbindt met de MySQL-server
- De juiste klant gevonden
- Maak een HTML-tabel, vul gegevens in en stuur het terug naar de 'txtHint' placeholder
- Vorige pagina AJAX XML
- Volgende pagina AJAX responseXML