Ajax transmits the JSON instance code and ajax transmits the json instance

Source: Internet
Author: User

Ajax transmits the JSON instance code and ajax transmits the json instance

Previous

Although ajax stands for asynchronous javascript and XML. However, when ajax technology is used, passing JSON has become a de facto standard. JSON is simple and convenient compared with XML. This article will rewrite the instance in the previous article and transmit data in JSON format.

Front-end page

<! -- Front-end page --> <! DOCTYPE html> 

Backend page

<? Php // function test_input ($ data) {$ data = trim ($ data); $ data = stripslashes ($ data ); $ data = htmlspecialchars ($ data); return $ data;} // set the html encoding format for the page Content to utf-8header ("Content-Type: application/json; charset = UTF-8 "); // defines a multi-dimensional array that contains employee information, each employee information is an array $ staff = array ("name" => "Hong Qi", "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 ')); // if the request is a get request, search for it. If the request is a POST request, then create // $ _ SERVER ["REQUEST_METHOD"] and return the request method if ($ _ SERVER ["REQUEST_METHOD"] = "GET") used to access the page ") {search ();} else if ($ _ SERVER ["REQUEST_METHOD"] = "POST") {create () ;}// search for employee function search () by employee ID () {// check whether the employee ID parameter exists // isset check whether the variable is set; empty checks whether 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 employee found"} '; // traverses the $ staff multi-dimensional array to check whether the employee with the key value of number exists. If yes, modify the returned result foreach ($ staff as $ value) {if ($ value ['number'] = $ number) {$ result = '{"success ": true, "msg": "locate EMPLOYEE: employee ID '. $ value ["number"]. ', employee name is '. $ value ["name"]. ', employee gender '. $ value ["sex"]. ', employee position is '. $ value ["job"]. '"}'; break;} echo $ result;} // create employee function create () {// determine whether the information is completely filled in 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 is incomplete"} '; return;} echo' {"success": true, "msg": "'. test_input ($ _ POST ['name']). 'The information is saved successfully! "} ';}?>

Instance demo

The above section describes how to transmit JSON instance code through Ajax. I hope it will be helpful to you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!

Related Article

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.