PHP development basic tutorial AJAX and MySQL
AJAX database instance
AJAX can be used to communicate interactively with the database
The following The example will demonstrate how a web page reads information from the database via AJAX
Please select a customer in the drop-down list on the left:
This example consists of four elements:
MySQL database
Simple HTML form
JavaScript
PHP Page
Database
This example requires the following data tables to be created in the database:
##HTML form and JavaScript
## for the source code #Source code explanation
选择相应用户,用户信息将在这里展示出来
After the user selects through the drop-down list, the showUser() function is executed through the onchange event
The showUser() function will perform the following steps:
-
Check if a user is selected
- Create an XMLHttpRequest object
- Create a function to be executed when the server response is ready
- Send a request to a file on the server
- Please note the parameter (q) added to the end of the URL (contains the contents of the drop-down list)
##PHP page
The above The server page called via JavaScript is a PHP file named "2.php". The source code in
"; //循环显示出用信息 while($row = mysqli_fetch_array($result)){ echo " 姓 名 年龄 家乡 工作 "; echo " "; } echo ""; ?>".$row['FirstName']." "; echo "".$row['LastName']." "; echo "".$row['Age']." "; echo "".$row['Hometown']." "; echo "".$row['Job']." "; echo "
Learning experience
This example mainly includes the following knowledge points:
表 Form Basis: Pulling option## On onchange Event: When the content of the domain changes,
Series can be called, function transfer value
# Ajax xmlhttprequest objects, when the server responds The function of executing, sending requests to the file on the server: See 1-5 Learning Experience
# steal Dom GetelementByid () Method: Return to quoting the first object of the designated ID
Create the database, connect to the database, select the database, set the character set, query from the database based on id, and loop out the contents of the database
Functions related to the database:
mysqli_connect(): Open a new connection to the MySQL server
mysqli_error(): Return to the previous A text error message generated by a MySQL operation.
mysqli_select_db(): used to change the default database for the connection
mysqli_query(): execute a query against the database
mysqli_fetch_array(): Get a row from the result set as an associative array, a numeric array, or both