用Json實現(xiàn)PHP與JavaScript間數(shù)據(jù)交換的方法詳解
更新時間:2013年06月20日 11:22:46 作者:
本篇文章是對用Json實現(xiàn)PHP與JavaScript間數(shù)據(jù)交換的方法進行了詳細的分析介紹,需要的朋友參考下
JSON(JavaScript Object Notation) 是一種輕量級的數(shù)據(jù)交換格式。
簡而論之,不管是xml還是json都是為了方便在客戶端與服務(wù)器端交互數(shù)據(jù)的中轉(zhuǎn)站,特別是用于對象型數(shù)據(jù),比如最常見的數(shù)組。
下面將分別將數(shù)組從php傳送給javascript,以及將數(shù)組從javascript傳送給php示例說明,例子比較簡單,明白概念即可。不管從php傳送給javascript,還是javascript傳送給php,json在傳送之前都會將對象扁平化即一維化為字符串。
PHP 向 JavaScript 傳值
PHP 文件 json.php
<?php
$arr = array(
'name' => '腳本之家',
'nick' => 'Gonn',
'contact' => array(
'email' => 'xxxxxxx@163.com',
'website' => 'http://www.dbjr.com.cn',
)
);
$json_string = json_encode($arr);
echo "getProfile($json_string)";
?>
光執(zhí)行這個文件,其結(jié)果如下:
getProfile({"name":"u5e0cu4e9a","nick":"Gonn",
"contact":{"email":"xxxxxxx@163.com","website":"http://www.dbjr.com.cn"}})
json.php 是通過 json_encode 函數(shù)將數(shù)組扁平化,然后發(fā)送,相反有個 json_decode 函數(shù)。
那么在 JavaScript 如何調(diào)用呢?很簡單,定義一個變量獲取 PHP 傳來的 Json,該 Json 具備對象的特性,我們可以用 array.name 這種方式來獲取該 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>
運行結(jié)果如下:
腳本之家
Gonn
xxxxxxx@163.com
http://www.dbjr.com.cn
JavaScript 向 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>
這里javascript扁平化需要一個插件:http://www.json.org/json2.js,通過JSON.stringify(str)將對象扁平化然后傳送給php。
注:另有一個http://www.json.org/json.js,對應(yīng)的是toJSONString方法。
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 />';
?>
這里就需要用到j(luò)son_decode()這個函數(shù),然后調(diào)用其中數(shù)據(jù)用 $obj->屬性即可。
簡而論之,不管是xml還是json都是為了方便在客戶端與服務(wù)器端交互數(shù)據(jù)的中轉(zhuǎn)站,特別是用于對象型數(shù)據(jù),比如最常見的數(shù)組。
下面將分別將數(shù)組從php傳送給javascript,以及將數(shù)組從javascript傳送給php示例說明,例子比較簡單,明白概念即可。不管從php傳送給javascript,還是javascript傳送給php,json在傳送之前都會將對象扁平化即一維化為字符串。
PHP 向 JavaScript 傳值
PHP 文件 json.php
復(fù)制代碼 代碼如下:
<?php
$arr = array(
'name' => '腳本之家',
'nick' => 'Gonn',
'contact' => array(
'email' => 'xxxxxxx@163.com',
'website' => 'http://www.dbjr.com.cn',
)
);
$json_string = json_encode($arr);
echo "getProfile($json_string)";
?>
光執(zhí)行這個文件,其結(jié)果如下:
復(fù)制代碼 代碼如下:
getProfile({"name":"u5e0cu4e9a","nick":"Gonn",
"contact":{"email":"xxxxxxx@163.com","website":"http://www.dbjr.com.cn"}})
json.php 是通過 json_encode 函數(shù)將數(shù)組扁平化,然后發(fā)送,相反有個 json_decode 函數(shù)。
那么在 JavaScript 如何調(diào)用呢?很簡單,定義一個變量獲取 PHP 傳來的 Json,該 Json 具備對象的特性,我們可以用 array.name 這種方式來獲取該 Json 的屬性。
復(fù)制代碼 代碼如下:
<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>
運行結(jié)果如下:
復(fù)制代碼 代碼如下:
腳本之家
Gonn
xxxxxxx@163.com
http://www.dbjr.com.cn
JavaScript 向 PHP 傳值
json_encode.html
復(fù)制代碼 代碼如下:
<!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>
這里javascript扁平化需要一個插件:http://www.json.org/json2.js,通過JSON.stringify(str)將對象扁平化然后傳送給php。
注:另有一個http://www.json.org/json.js,對應(yīng)的是toJSONString方法。
復(fù)制代碼 代碼如下:
var last=obj.toJSONString(); //針對json.js
var last=JSON.stringify(obj); //針對json2.js
json_encode.php
復(fù)制代碼 代碼如下:
<?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 />';
?>
這里就需要用到j(luò)son_decode()這個函數(shù),然后調(diào)用其中數(shù)據(jù)用 $obj->屬性即可。
您可能感興趣的文章:
- PHP的Laravel框架結(jié)合MySQL與Redis數(shù)據(jù)庫的使用部署
- CentOS 安裝 PHP5.5+Redis+XDebug+Nginx+MySQL全紀錄
- PHP使用redis實現(xiàn)統(tǒng)計緩存mysql壓力的方法
- docker搭建php+nginx+swoole+mysql+redis環(huán)境的方法
- php中關(guān)于codeigniter的xmlrpc的類在進行數(shù)據(jù)交換時的類型問題
- PHP+Redis開發(fā)的書簽案例實戰(zhàn)詳解
- php結(jié)合redis實現(xiàn)高并發(fā)下的搶購、秒殺功能的實例
- php+redis實現(xiàn)商城秒殺功能
- php+redis消息隊列實現(xiàn)搶購功能
- PHP結(jié)合Redis+MySQL實現(xiàn)冷熱數(shù)據(jù)交換應(yīng)用案例詳解
相關(guān)文章
用PHP ob_start()控制瀏覽器cache、生成html實現(xiàn)代碼
Output Control 函數(shù)可以讓你自由控制腳本中數(shù)據(jù)的輸出。它非常地有用,特別是對于:當你想在數(shù)據(jù)已經(jīng)輸出后,再輸出文件頭的情況。2010-02-02PHP實現(xiàn)生成數(shù)據(jù)字典功能示例
這篇文章主要介紹了PHP實現(xiàn)生成數(shù)據(jù)字典功能,涉及php針對mysql常見的連接、數(shù)據(jù)表查詢、遍歷、table表格構(gòu)成等相關(guān)操作技巧,需要的朋友可以參考下2018-05-05dir()、readdir()、scandir()和glob()四種遍歷目錄方法及性能分析
php遍歷目錄和文件的場景在很多時候都能用到,遍歷目錄方法的方法有好幾種,那么應(yīng)該使用哪種方法呢?下面介紹dir()、readdir()、scandir()和glob()四種遍歷目錄方法及性能分析。2022-12-12php模仿asp Application對象在線人數(shù)統(tǒng)計實現(xiàn)方法
這篇文章主要介紹了php模仿asp Application對象在線人數(shù)統(tǒng)計實現(xiàn)方法,通過一個比較簡單的自定義函數(shù)實現(xiàn)這一功能,具有一定參考借鑒價值,需要的朋友可以參考下2015-01-01PHP中調(diào)試函數(shù)debug_backtrace的使用示例代碼
debug_backtrace() 是一個很低調(diào)的函數(shù),很少有人注意過它,這篇文章主要給大家介紹了關(guān)于PHP中調(diào)試函數(shù)debug_backtrace的使用方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,感興趣的朋友們隨著小編來一起學習學習吧。2017-09-09php+ajax登錄跳轉(zhuǎn)登錄實現(xiàn)思路
這篇文章主要介紹了php+ajax登錄跳轉(zhuǎn)登錄實現(xiàn)思路,非常的簡單,有需要的小伙伴可以參考下2016-07-07