A method to implement data exchange between PHP and JavaScript with JSON _php tips

Source: Internet
Author: User
JSON (JavaScript Object notation) is a lightweight data interchange format.
In Jane's view, both XML and JSON are meant to facilitate the interchange of data between client and server, especially for object-type data, such as the most common arrays.

The following will pass the array from PHP to JavaScript, and the array from JavaScript to the PHP sample description, the example is relatively simple, understand the concept. Whether it's transferred from PHP to JavaScript or JavaScript to Php,json, the object is flattened into a string before it is transmitted.
PHP passes values to JavaScript
PHP File json.php
Copy Code code as follows:

<?php
$arr = Array (
' Name ' => ' cloud-dwelling community ',
' Nick ' => ' Gonn ',
' Contact ' => Array (
' Email ' => ' xxxxxxx@163.com ',
' website ' => ' http://www.jb51.net ',
)
);
$json _string = Json_encode ($arr);
echo "GetProfile ($json _string)";
?>

Light to execute this file, the results are as follows:
Copy Code code as follows:

GetProfile ({"Name": "u5e0cu4e9a", "Nick": "Gonn",
"Contact": {"email": "xxxxxxx@163.com", "website": "Http://www.jb51.net"})

Json.php is to flatten the array through the Json_encode function and then send it, instead of having a json_decode function.
So how do you call JavaScript? It's very simple to define a variable to get the JSON from PHP, which has the attributes of the object, and we can get the JSON's properties in Array.name this way.
Copy Code code 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 results of the operation are as follows:
Copy Code code as follows:

Cloud-dwelling communities
Gonn
Xxxxxxx@163.com
Http://www.jb51.net

JavaScript is passing values to PHP
json_encode.html
Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<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>

<body>

<form id= "Form1" Name= "Form1" "method=" Post "action=" json_encode.php "onsubmit=" json_test (this); >
<label for= "Txt_name" > Name </label>
<p><input type= "text" name= "Txt_name" id= "Txt_name"/></p>
<label for= "Txt_email" > Mailbox </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>

Here JavaScript flattening requires a plugin: Http://www.json.org/json2.js, flattening the object through Json.stringify (str) and sending it to PHP.
Note: There is another http://www.json.org/json.js, corresponding to the toJSONString method.
Copy Code code as follows:

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

json_encode.php
Copy Code code 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/><br/><br/><br/> ';
echo $user->name. ' <br/> ';
echo $user->email. ' <br/> ';
echo $user->password. ' <br/> ';
?>

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

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.