PHP - AJAX and MySQL
PHP - AJAX and MySQL
AJAX can be used to communicate interactively with the database.
AJAX Database Example
The following example will demonstrate how a web page reads information from the database through AJAX:
Example
Explanation of examples - MySQL database
In the above example, the database table we use is as follows:
Explanation of examples - HTML page
When the user selects a user in the drop-down list above, the name "showUser()" will be executed " The function. This function is triggered by the "onchange" event:
showUser() function will perform the following steps:
·
· Create a function that executes when the server response is ready Content)
PHP fileThe server page called above through JavaScript is a PHP file named "getuser.php".
The source code in "getuser.php" runs a query against the MySQL database and returns the results in an HTML table:
Explanation: When the query is sent from JavaScript to the PHP file , will happen:"; while($row = mysqli_fetch_array($result)) { echo " Firstname Lastname AgeHometown Job "; echo " "; } echo ""; mysqli_close($con); ?>" . $row['FirstName'] . " "; echo "" . $row['LastName'] . " "; echo "" . $row['Age'] . " "; echo "" . $row['Hometown'] . " "; echo "" . $row['Job'] . " "; echo "
1. PHP opens a connection to the MySQL database
2. Find the selected user
Create an HTML table, fill in the data, and send back "txtHint " Placeholder