PHP - AJAX with MySQL
AJAX can be used to communicate interactively with a database.
AJAX Database Example
The example below demonstrates how a web page reads information from a database through AJAX:
The SQL file for the Websites table used in this tutorial:websites.sql.
Example
Select the corresponding option, and the user information will be displayed here...
Example Explanation - MySQL Database
In the example above, the database table we use is as follows:
mysql> select * from websites;
+----+--------------+---------------------------+-------+---------+
| id | name | url | alexa | country |
+----+--------------+---------------------------+-------+---------+
| 1 | Google | https://www.google.cm/ | 1 | USA |
| 2 | 淘宝 | https://www.taobao.com/ | 13 | CN |
| 3 | Runoob Tutorial | http://www.runoob.com/ | 4689 | CN |
| 4 | 微博 | http://weibo.com/ | 20 | CN |
| 5 | Facebook | https://www.facebook.com/ | 3 | USA |
+----+--------------+---------------------------+-------+---------+
5 rows in set (0.01 sec)
Example Explanation - HTML Page
When the user selects a user in the dropdown list above, a function named "showSite()" is executed. This function is triggered by the "onchange" event:
test.html file code:
<!DOCTYPE html><html>
<head>
<meta charset="utf-8">
<title>Runoob Tutorial(runoob.com)
title
<script function showSite(str){if(str=="" ){document>.
getElementById
(
"
txtHint
"
)
.
innerHTML
=
"
"
;
return
;
}
if
(
window
.
XMLHttpRequest
)
{
//
IE7+, Firefox, Chrome, Opera, Safari browser execution code
xmlhttp
=
new
XMLHttpRequest
(
)
;
}
else
{
//
IE6, IE5 browser execution code
xmlhttp
=
new
ActiveXObject
(
"
Microsoft.XMLHTTP
"
)
;
}
xmlhttp
.
onreadystatechange
=
function
(
)
{
if
(
xmlhttp
.
readyState
==
4
&&
xmlhttp
.
status
==
200
)
{
document
.
getElementById
(
"
txtHint
"
)
.
innerHTML
=
xmlhttp
.
responseText
;
}
}
xmlhttp
.
open
(
"
GET
"
,
"
getsite_mysql.php?q=
"
str
,
true
)
;
xmlhttp
.
send
(
)
;
}
script
head
<body>
<form>
<select name="users" onchange="showSite(this.value)">
<option value="">Select a website:
option
<option value="1" Google option>
<option value="2" Taobao option>
<option value="3">Runoob Tutorial
option
<option value="4" Weibo option>
<option value="5" Facebook option select form>
<br>
<div id="txtHint">
<b>Website information is displayed here...
b
div
body
html
The showSite() function performs the following steps:
- Check whether a website is selected
- Create an XMLHttpRequest object
- Create the function to be executed when the server response is ready
- Send a request to the file on the server
- Note the parameter (q) appended to the end of the URL (containing the dropdown list contents)
PHP File
The server page called via JavaScript above is a PHP file named "getsite_mysql.php".
The source code in "getsite_mysql.php" runs a query against the MySQL database and then returns the result in an HTML table:
getsite_mysql.php file code:
ID Site Name Site URL Alexa Rank Country " ; while ( $row = mysqli_fetch_array ( $result ) ) { echo " " ; echo " " . $row [ ' id ' ] . " " ; echo " " . $row [ ' name ' ] . " " ; echo " " . $row [ ' url ' ] . " " ; echo " " . $row [ ' alexa ' ] . " " ; echo " " . $row [ ' country ' ] . " " ; echo " " ; } echo " " ; mysqli_close ( $con ) ; ?>Explanation: When the query is sent from JavaScript to the PHP file, the following happens:
- PHP opens a connection to the MySQL database
- Find the selected user
- Create an HTML table, fill it with data, and send it back to the "txtHint" placeholder