Home  >  Article  >  Backend Development  >  Using Json to implement data exchange between PHP and JavaScript_PHP tutorial

Using Json to implement data exchange between PHP and JavaScript_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:33:25908browse

JSON (JavaScript Object Notation) is a lightweight data exchange format.

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

The following are examples of transferring arrays from php to javascript, and transferring arrays from javascript to php. The examples are relatively simple, and you only need to understand the concept. Regardless of whether it is transmitted from php to javascript or javascript to php, json will flatten the object before transmitting it, that is, one-dimensionally convert it into a string.

PHP passes value to JavaScript

PHP file json.php

<?php
	$arr = array(
		'name' => '希亚',
		'nick' => 'Gonn',
		'contact' => array(
			'email' => 'gonnsai@163.com',
			'website' => 'http://www.bkjia.com',
		)
	);
	$json_string = json_encode($arr);
	echo "getProfile($json_string)";
?>

Just execute this file, the result is as follows:

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

json.php uses the json_encode function to flatten the array and then send it. On the contrary, there is a json_decode function.

So how to call it in JavaScript? It's very simple. Define a variable to get the Json sent by PHP. The Json has the characteristics of an object. We can use array.name to get the properties of the Json.

<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 results are as follows:

希亚
Gonn
gonnsai@163.com
http://www.bkjia.com

JavaScript passes value to PHP

json_encode.html

<!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("点击确定后将提交表单");
	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">姓名</label>
		<p><input type="text" name="txt_name" id="txt_name" /></p>
		<label for="txt_email">邮箱</label>
		<p><input type="text" name="txt_email" id="txt_email" /></p>
		<p><label for="txt_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, which flattens the object through JSON.stringify(str) and then transmits it to php.

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

var last=obj.toJSONString(); //针对json.js
var last=JSON.stringify(obj); //针对json2.js

json_encode.php

<?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 using the $obj-> attribute.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/752487.htmlTechArticleJSON (JavaScript Object Notation) is a lightweight data exchange format. In short, both xml and json are transfer stations to facilitate the exchange of data between the client and the server...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn