Previous words
Although the Ajax full name is asynchronous JavaScript and XML. However, when using AJAX technology, passing JSON has become a de facto standard. Because JSON is simple and convenient compared to XML. This article overwrites the instance in the previous article to pass the data in JSON
Front Page
<!--front page--><! DOCTYPE html>Back-end Page<?php//For filtering unsafe character function Test_input ($data) {$data = Trim ($data); $data = Stripslashes ($data); $data = Htmlspecialchars ($data); return $data;} The HTML Encoding format for setting page content is Utf-8header ("Content-type:application/json;charset=utf-8");//define a multidimensional array that contains information about employees, each employee information is an array of $ Staff = Array ("Name" and "Hong Seven", "number" = "101", "sex" = "male", "job" = "General Manager"), Array ("name" = "Guo Jing", " Number "=" 102 "," sex "=" male "," job "=" Development Engineer ", Array (" name "=" Huang Rong "," number "=" 103 "," sex "=" female "," job "= = ' product manager ');//Determine if a GET request is searched, or if it is a POST request, create a new//$_server[" Request_method ") to return the request method used by the access page if ($_server[" request _method "] = =" GET ") {search ();} else if ($_server["request_method"] = = "POST") {Create ();} Search employee by Employee ID () {//Check if there is an employee number parameter//isset detect if the variable is set; empty determines if the value is null if (!isset ($_get[' number ')) | | empty ($_ get[' number ']) {echo ' {"Success": false, "MSG": "Parameter Error"} '; Return } Global $staff; $number = Test_input ($_get[' number '); $result = ' {' success ': false, ' mSG ":" No Employees Found "}"; Traverse the $staff multidimensional array to find out whether an employee with a key value of number exists. If present, modify the returned result foreach ($staff as $value) {if ($value [' number '] = = $number) {$result = ' {"Success": true, " MSG ":" Find Employee: Employee number is '. $value ["No."]. ', the employee's name is '. $value ["name"]. ' The gender of the employee is '. $value ["Sex"]. ', employee positions are '. $value ["Job"]. Break }} echo $result;} Create an employee function create () {//To determine if the information is filled in exactly if (!isset ($_post[' name ')) | | empty ($_post[' name ']) | | !isset ($_post[' number ') | | Empty ($_post[' number ') | | !isset ($_post[' sex ') | | Empty ($_post[' sex ') | | !isset ($_post[' job ') | | Empty ($_post[' job ')) {echo ' {"Success": false, "MSG": "Parameter error, employee information not complete"} "; Return } Echo ' {' success ': true, ' msg ': ' Employee '. Test_input ($_post[' name ']). ' Information saved successfully! "} ';}? >
Example DemoIn-depth understanding of the seventh Ajax series