jquery與php的json交互

jsonPHPjQueryMySQLAjaxjavascript

整理兩個現成的函數:json_decode、json_encode
說明:其中json_encode 表示把經常使用的傳統的數據類型如對象、數組、關聯數組等轉成JSON字符串。其實與JAVA裏面的那個工具是同樣的。而json_decode恰好相反。
解決需求1.修改
整理兩個現成的函數:json_decode、json_encode
說明:其中json_encode 表示把經常使用的傳統的數據類型如對象、數組、關聯數組等轉成JSON字符串。其實與JAVA裏面的那個工具是同樣的。而json_decode恰好相反。
解決需求1.修改數據表的時候動態生成一個JSON片斷。供JS調用。
服務器端的代碼:
function plan2() {
$link = mysql_connect("localhost","root","123") or die("<font color=red>沒法創建起來鏈接。錯誤信息以下</font>");
mysql_query("SET NAMES gbk");
mysql_select_db("phpcms",$link) or die("<font color=red>在服務器上面沒法找到此請確認已創建此DB ");
$result = mysql_query("select id,uuid,uuidtable from dytable ");
$num_rows = @mysql_num_rows($result); //看一下返回多少行記錄
if ($num_rows == 0) {
    $b = array();         //這樣長度爲0 返回的是一個空數組             
}else{         
    while ($row = mysql_fetch_array($result,MYSQL_ASSOC)){
        $b[] = $row;
    }
}
echo json_encode($b);
mysql_close();
}
plan2();

這樣生成的JSON是比較方便的了!

2. 客戶端若是咱們使用JQuery框架的話能夠這樣處理
<script type="text/javascript">
function ajaxcheck() {
    $.ajax({
        type:"GET",
        url: "http://localhost/PHPCMS/projcode/?number="+Math.random(),
        dataType: 'text',           //注意這裏面的格式形式!
        success:function(msg){
           $(eval_r(msg)).each(function(){
                alert(this.id+" "+this.uuid);//獲得值就能夠生成多個IMG標籤了!
            });
           
        }
    })
}
</script>

若是客戶端使用JS的話能夠這樣處理
<script type="text/javascript">
function ajaxcheck() {
    $.ajax({
        type:"GET",
       url: "http://localhost/PHPCMS/projcode/?number="+Math.random(),
        dataType: 'text',
        success:function(msg){        
            json = eval_r(msg)
            for(var i=0; i<json.length; i++)
            {
            alert(json[i].id+" " + json[i].uuid)
            }
        }
    })
}
</script>



參考的一個示例代碼以下:
客戶端代碼:
<!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>
<script type="text/javascript" src="../scripts/jquery.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
function ajaxcheck() {
    $.ajax({
        type:"GET",
        url: "http://localhost/PHPCMS/projcode/?number="+Math.random(),
        dataType: 'text',
        success:function(msg){
            $(eval_r(msg)).each(function(){
                $("#output").html("<img id='"+this.id+"' src='"+this.uuid+"' />");
            });
        }
    })
}
</script>
</head>
<body>
<button onclick="ajaxcheck()">TEST</button>
<div id="output"></div>
</body>
</html>

服務端:
function plan2() {
$link = mysql_connect("localhost","root","123") or die("<font color=red>沒法創建起來鏈接。錯誤信息以下</font>");
mysql_query("SET NAMES gbk");
mysql_select_db("phpcms",$link) or die("<font color=red>在服務器上面沒法找到此請確認已創建此DB ");
$result = mysql_query("select id,uuid from dytable ");
$num_rows = @mysql_num_rows($result); //看一下返回多少行記錄
if ($num_rows == 0) {
    $b = array();         //這樣長度爲0 返回的是一個空數組             
}else{         
    while ($row = mysql_fetch_array($result,MYSQL_ASSOC)){
        $b[] = $row;
    }
}
echo json_encode($b);
mysql_close();
}
plan2();

以上實現的功能是藉助JSON實現的。其實FLASH也有函數解析JSONphp

相關文章
相關標籤/搜索