_php tutorials for data exchange between PHP and JavaScript using JSON

Source: Internet
Author: User
JSON (JavaScript Object Notation) is a lightweight data interchange format.

In brief, both XML and JSON are used to facilitate the transfer of data between client and server, especially for object-type data, such as the most common arrays.

The following will send the array from PHP to JavaScript separately, and the array from JavaScript to the PHP sample description, the example is relatively simple, understand the concept can be. Whether it is sent from PHP to JavaScript or JavaScript to Php,json, the object is flattened and then transformed into a string before being transmitted.

PHP value to JavaScript

PHP file json.php

 
  ' Shia ', ' nick ' = ' gonn ', ' contact ' + = array (' email ' = ' gonnsai@163.com ', ' website ' = ' http://www.bkjia.com ', ); $json _string = Json_encode ($arr); echo "GetProfile ($json _string)";? >

The results of this file are as follows:

GetProfile ({"Name": "u5e0cu4e9a", "Nick": "Gonn", "contact": {"email": "gonnsai@163.com", "website": "Http://www.bkjia.com"}})

Json.php is a json_encode function that flattens an array and then sends it, instead of a json_decode function.

So how do you call it in JavaScript? It's very simple to define a variable to get the JSON from PHP, which has the properties of the object, and we can get the properties of the JSON in Array.name this way.

   

The results of the operation are as follows:

Shia gonngonnsai@163.comhttp://www.bkjia.com

JavaScript passes value to PHP

Json_encode.html

 
  Json:from JavaScript to PHP   

Here JavaScript flattening requires a plug-in: Http://www.json.org/json2.js, flattening the object through Json.stringify (str) and then passing it to PHP.

Note: There is another http://www.json.org/json.js, corresponding to the toJSONString method.

var last=obj.tojsonstring (); For Json.jsvar last=json.stringify (obj); For Json2.js

json_encode.php

 
  


' Echo $user->name. '
' Echo $user->email. '
' Echo $user->password. '
';? >

Here you need to use the Json_decode () function, and then call the data with the $obj-so property.

http://www.bkjia.com/PHPjc/752487.html www.bkjia.com true http://www.bkjia.com/PHPjc/752487.html techarticle JSON (JavaScript Object Notation) is a lightweight data interchange format. In brief, both XML and JSON are designed to facilitate the interaction of data between the client and the server .

  • 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.