亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

用Json實現(xiàn)PHP與JavaScript間數(shù)據(jù)交換的方法詳解

 更新時間:2013年06月20日 11:22:46   作者:  
本篇文章是對用Json實現(xiàn)PHP與JavaScript間數(shù)據(jù)交換的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
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
復(fù)制代碼 代碼如下:

<?php
     $arr = array(
         'name' => '腳本之家',
         'nick' => 'Gonn',
         'contact' => array(
             'email' => 'xxxxxxx@163.com',
             'website' => 'http://chabaoo.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://chabaoo.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://chabaoo.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->屬性即可。

相關(guān)文章

  • PHP中Memcache操作類及用法實例

    PHP中Memcache操作類及用法實例

    這篇文章主要介紹了PHP中Memcache操作類及用法,以實例形式詳細(xì)分析了Memcache類連接數(shù)據(jù)庫及進(jìn)行緩存操作的具體用法,非常具有實用價值,需要的朋友可以參考下
    2014-12-12
  • 用PHP ob_start()控制瀏覽器cache、生成html實現(xiàn)代碼

    用PHP ob_start()控制瀏覽器cache、生成html實現(xiàn)代碼

    Output Control 函數(shù)可以讓你自由控制腳本中數(shù)據(jù)的輸出。它非常地有用,特別是對于:當(dāng)你想在數(shù)據(jù)已經(jīng)輸出后,再輸出文件頭的情況。
    2010-02-02
  • CodeIgniter上傳圖片成功的全部過程分享

    CodeIgniter上傳圖片成功的全部過程分享

    以下小編為大家分享一下使用CodeIgniter上傳圖片成功的全部過程。需要的朋友可以過來參考一下哦
    2013-08-08
  • PHP實現(xiàn)生成數(shù)據(jù)字典功能示例

    PHP實現(xiàn)生成數(shù)據(jù)字典功能示例

    這篇文章主要介紹了PHP實現(xiàn)生成數(shù)據(jù)字典功能,涉及php針對mysql常見的連接、數(shù)據(jù)表查詢、遍歷、table表格構(gòu)成等相關(guān)操作技巧,需要的朋友可以參考下
    2018-05-05
  • PHP之uniqid()函數(shù)用法

    PHP之uniqid()函數(shù)用法

    這篇文章主要介紹了PHP中uniqid()函數(shù)的用法,包括了函數(shù)的基本用法與應(yīng)用分析,是非常實用的技巧,需要的朋友可以參考下
    2014-11-11
  • dir()、readdir()、scandir()和glob()四種遍歷目錄方法及性能分析

    dir()、readdir()、scandir()和glob()四種遍歷目錄方法及性能分析

    php遍歷目錄和文件的場景在很多時候都能用到,遍歷目錄方法的方法有好幾種,那么應(yīng)該使用哪種方法呢?下面介紹dir()、readdir()、scandir()和glob()四種遍歷目錄方法及性能分析。
    2022-12-12
  • php模仿asp Application對象在線人數(shù)統(tǒng)計實現(xiàn)方法

    php模仿asp Application對象在線人數(shù)統(tǒng)計實現(xiàn)方法

    這篇文章主要介紹了php模仿asp Application對象在線人數(shù)統(tǒng)計實現(xiàn)方法,通過一個比較簡單的自定義函數(shù)實現(xiàn)這一功能,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-01-01
  • php使用curl簡單抓取遠(yuǎn)程url的方法

    php使用curl簡單抓取遠(yuǎn)程url的方法

    這篇文章主要介紹了php使用curl簡單抓取遠(yuǎn)程url的方法,涉及php操作curl的技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-03-03
  • PHP中調(diào)試函數(shù)debug_backtrace的使用示例代碼

    PHP中調(diào)試函數(shù)debug_backtrace的使用示例代碼

    debug_backtrace() 是一個很低調(diào)的函數(shù),很少有人注意過它,這篇文章主要給大家介紹了關(guān)于PHP中調(diào)試函數(shù)debug_backtrace的使用方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,感興趣的朋友們隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-09-09
  • php+ajax登錄跳轉(zhuǎn)登錄實現(xiàn)思路

    php+ajax登錄跳轉(zhuǎn)登錄實現(xiàn)思路

    這篇文章主要介紹了php+ajax登錄跳轉(zhuǎn)登錄實現(xiàn)思路,非常的簡單,有需要的小伙伴可以參考下
    2016-07-07

最新評論