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.