In-depth understanding of the seventh Ajax series

Source: Internet
Author: User

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 Demo

In-depth understanding of the seventh Ajax series

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.