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

QQ图片20161010102648.png

Explanation of examples - MySQL database

In the above example, the database table we use is as follows:

QQ图片20161010102703.png

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:

   

Person info will be listed here.

showUser() function will perform the following steps:

·

· Create a function that executes when the server response is ready Content)

PHP file

The 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:

  Firstname LastnameAge Hometown Job "; while($row = mysqli_fetch_array($result)) { echo ""; echo "" . $row['FirstName'] . ""; echo "" . $row['LastName'] . ""; echo "" . $row['Age'] . ""; echo "" . $row['Hometown'] . ""; echo "" . $row['Job'] . ""; echo ""; } echo ""; mysqli_close($con); ?>
Explanation: When the query is sent from JavaScript to the PHP file , will happen:

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

Continuing Learning
||

Person info will be listed here.
submit Reset Code
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!