Use Json to implement data exchange between PHP and JavaScript _ PHP Tutorial

Source: Internet
Author: User
Use Json to exchange data between PHP and JavaScript. JSON (JavaScriptObjectNotation) is a lightweight data exchange format. In short, both xml and json are used to facilitate the interaction between the client and the server. JavaScript Object Notation is a lightweight data exchange format.

In short, both xml and json are used to facilitate data exchange between the client and the server, especially for object-type data, such as the most common array.

The following example transfers the array from php to javascript and the array from javascript to php. The example is simple and you can understand the concept. No matter whether it is sent from php to javascript or javascript to php, json will flat the object, that is, convert one dimension into a string before transmission.

PHP transfers values to JavaScript

Json. PHP file

 'Sha', 'Nick '=> 'gonn', 'Contact' => array ('email '=> 'gonngi @ 163.com', 'Website' => 'http: // www.bkjia.com ',); $ json_string = json_encode ($ arr); echo "getProfile ($ json_string)";?>

The result of executing this file is as follows:

getProfile({"name":"u5e0cu4e9a","nick":"Gonn","contact":{"email":"gonnsai@163.com","website":"http://www.bkjia.com"}})

Json. php flat the array through the json_encode function, and then sends it. on the contrary, there is a json_decode function.

So how can we call JavaScript? It's easy to define a variable to get Json from PHP. this Json has the object features. we can use array. name to get the Json attributes.

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.