How to exchange data between PHP and JavaScript using Json

Source: Internet
Author: User

JSON (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
Copy codeThe Code is as follows:
<? Php
$ Arr = array (
'Name' => 'foot home ',
'Nick '=> 'gonn ',
'Contact '=> array (
'Email '=> 'xxxxxxx @ 163.com ',
'Website' => 'HTTP: // www.jb51.net ',
)
);
$ Json_string = json_encode ($ arr );
Echo "getProfile ($ json_string )";
?>

The result of executing this file is as follows:
Copy codeThe Code is as follows:
GetProfile ({"name": "u5e0cu4e9a", "nick": "Gonn ",
"Contact": {"email": "xxxxxxx@163.com", "website": "http://www.jb51.net "}})

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.
Copy codeThe Code is as follows:
<Script type = "text/javascript">
Function getProfile (str ){
Var arr = str;
Document. getElementById ('name'). innerHTML = arr. name;
Document. getElementById ('Nick '). innerHTML = arr. nick;
Document. getElementById ('email '). innerHTML = arr. contact. email;
Document. getElementById ('website'). innerHTML = arr. contact. website;
}
</Script>
<Body>
<Div id = "name"> </div>
<Div id = "nick"> </div>
<Div id = "email"> </div>
<Div id = "website"> </div>
</Body>
<Script type = "text/javascript" src = "json. php"> </script>

The running result is as follows:
Copy codeThe Code is as follows:
Foot home
Gonn
Xxxxxxx@163.com
Http://www.jb51.net

JavaScript transfers values to PHP
Json_encode.html
Copy codeThe Code is as follows:
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> json: From javascript To php </title>
<Script src = "json2.js" type = "text/javascript"> </script>
<Script type = "text/javascript">
Function JSON_test (o)
{
Var user = {
Name: document. getElementById ('txt _ name'). value,
Email: document. getElementById ('txt _ email '). value,
Password: document. getElementById ('txt _ password'). value
}
Var json_string = JSON. stringify (user );
Document. getElementById ('txt _ json'). value = json_string;
Alert ("Click OK to submit the form ");
O. submit ();
}
</Script>
</Head>

<Body>

<Form id = "form1" name = "form1" method = "post" action = "json_encode.php" onsubmit = "JSON_test (this); return flase;">
<Label for = "txt_name"> name </label>
<P> <input type = "text" name = "txt_name" id = "txt_name"/> </p>
<Label for = "txt_email"> email address </label>
<P> <input type = "text" name = "txt_email" id = "txt_email"/> </p>
<P> <label for = "txt_password"> password </label> </p>
<P> <input type = "text" name = "txt_password" id = "txt_password"/> </p>
<P> <input type = "text" name = "txt_json" id = "txt_json"/>
<Label for = "button"> </label>
<Input type = "submit" name = "button" id = "button" value = "JSON"/>
</P>
</Form>

</Body>
</Html>

Here, javascript flattening requires a plug-in: http://www.json.org/json2.js. through json.stringify (str), the object is flattened and then transmitted to php.
Note: Another http://www.json.org/json.js corresponds to tojsonstring.pdf.
Copy codeThe Code is as follows:
Var last = obj. toJSONString (); // For json. js
Var last = JSON. stringify (obj); // For json2.js

Json_encode.php
Copy codeThe Code is as follows:
<? Php
Header ('content-Type: text/html; charset = UTF-8 ');
$ Json_string = $ _ POST ["txt_json"];
// Echo $ json_string;
If (ini_get ("magic_quotes_gpc") = "1 ")
{
$ Json_string = stripslashes ($ json_string );
}
$ User = json_decode ($ json_string );

Echo var_dump ($ user );

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

Here we need to use the json_decode () function, and then call the $ obj-> attribute for the data.

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.