2010-02-23 22 views
1
<?php 
include_once('db.php'); 
$location = $_POST['location']; 
$doctor = $_POST['doctor']; 
$patient_id = $_POST['patient_id']; 

if(($location != "") && ($doctor != "")) { 
    $sql = "select Name,Age,Gest_age,Weight from rop_form where Location = '".$location."' and Doctor = '".$doctor."' and Patient_id = '".$patient_id."'"; 
    $result = mysql_query($sql); 
    $myresult = ""; 
    while($row = mysql_fetch_array($result)) { 
    $myresult1['Patient_id'] = 'R'.$patient_id; 
    $myresult1['Name'] = $row['Name']; 
    $myresult1['Age'] = $row['Age']; 
    $myresult1['Weight'] = $row['Weight']; 
    $myresult1['Gest_age'] = $row['Gest_age']; 
    } 
    $myresult = json_encode($myresult1); 
} 
else { 
    $myresult .= ""; 
} 
echo $myresult; 
?> 

這是我的PHP代碼。jQuery數組問題

這是jQuery代碼。

$("#patient_id").change(function() { 
    $.post("/diabetes/patient_detail_change.php",{ location:$("#location").val(),doctor:$("#doctor").val(),patient_id:$("#patient_id").val()} ,function(json_data) { 
    alert(json_data); 
    //var my_json = //{"Patient_id":"R00020","Name":"admin","Age":"12","Weight":"67","Gest_age":"2"//}; 
    $.each(json_data, function(key,value) { 
     alert(key + ': ' + value); 
     if(key == 'Name'){ $("#name").val(value); } 
     if(key == 'Age'){ $("#age").val(value); } 
     if(key == 'Weight'){ $("#ropweight").val(value); } 
     if(key == 'Gest_age'){ $("#gest_age").val(value); } 
    }); 
    }); 
}); 

alert(json_data);該行正確打印像

{"Patient_id":"R00006","Name":"admin","Age":"12","Weight":"67","Gest_age":"2"}這是jQuery的

但是這是目前像。每個循環語句所需要的fomat:alert(key + ': ' + value);不打印像Patient_id:R00006和所有。但它打印像0:{ 1:P 2:a 3:t 4:i ..可能是什麼問題?

+0

不要忘記接受最適合您的答案。 – 2010-02-23 17:55:50

回答

2

除了馬特埃倫的回答,$.each()方法是用於遍歷JavaScript數組和陣列狀物體(即具有長度屬性)。 PHP的關聯數組(關鍵字 - >值)被轉換爲原生JavaScript對象。您可以使用for...in循環代替:

for (var key in json_data) { 
    alert(key + ': ' + json_data[key]); 
    if(key == 'Name'){ $("#name").val(json_data[key]);} 
    if(key == 'Age'){ $("#age").val(json_data[key]);} 
    if(key == 'Weight'){ $("#ropweight").val(json_data[key]);} 
    if(key == 'Gest_age'){ $("#gest_age").val(json_data[key]);} 
} 

但是,您可能不需要循環。您可以使用:

$.post (
    "/diabetes/patient_detail_change.php", 
    { 
    location:$("#location").val(), 
    doctor:$("#doctor").val(), 
    patient_id:$("#patient_id").val() 
    }, 
    function (json_data){ 
    if ("Name" in json_data) { $("#name").val(json_data.Name);} 
    if ("Age" in json_data) { $("#age").val(json_data.Age);} 
    if ("Weight" in json_data) { $("#ropweight").val(json_data.Weight);} 
    if ("Gest_age" in json_data) { $("#gest_age").val(json_data.Gest_age);} 
    }, 
    "json" 
); 
+0

好點,最好擺脫不必要的循環 – 2010-02-23 17:55:07

+0

$(「#patient_id」 ).change(function(){ $ .post( 「/diabetes/patient_detail_change.php」, { location:$(「#location」)。val(), doctor:$(「#doctor」) .val(), patient_id:$(「#patient_id」)。val() }, 函數(json_data){ alert(json_data); alert(json_data.Name); (json_data.Name){$(「#name」).val(json_data.Name);} if(json_data.Age){$(「#age」).val(json_data.Age);} if (json_data.Weight){$(「#ropweight」)。val(json_data。(json_data.Gest_age){$(「#gest_age」).val(json_data.Gest_age);} }, 「Json」 );} if }); – Hacker 2010-02-24 05:06:52

+0

我這樣做它仍然打印警報(json_data);正如{「Patient_id」:「R00006」,「Name」:「admin」,「Age」:「12」,「Weight」:「67」,「Gest_age」:「2」}但是 alert(json_data.Name );打印爲undefined :( – Hacker 2010-02-24 05:07:12

2

在你的文章中,你需要指定你正在返回JSON。

$.post("/diabetes/patient_detail_change.php",{location:$("#location").val(),doctor:$("#doctor").val(),patient_id:$("#patient_id").val()} ,function(json_data){ 

alert(json_data); 

$.each(json_data, function(key,value){ 
alert(key + ': ' + value); 
if(key == 'Name'){ $("#name").val(value);} 
if(key == 'Age'){ $("#age").val(value);} 
if(key == 'Weight'){ $("#ropweight").val(value);} 
if(key == 'Gest_age'){ $("#gest_age").val(value);} 

}); 
}, "json"); 

像這樣。

此刻,您的返回數據被視爲字符串,因此每個語句都會輸出每個字符。

看到這裏jQuery post definition

+0

+1,大約三秒鐘打我吧:( – karim79 2010-02-23 11:59:40

+0

我的熱切可能會留下bug ... – 2010-02-23 12:01:55

1

定義您應該使用 $.post ("/diabetes/patient_detail_change.php",{ location:$("#location").val(),doctor:$("#doctor").val(),patient_id:$("#patient_id").val()}, function (json_data){ //blah blah },"Json");

請檢查的最後一個參數 「的Json」

+0

我做到了,並在最後添加了json。但它的打印方式與之前相同:( – Hacker 2010-02-23 12:08:06

+1

打開firebug goto Net - > xhr標籤並檢查json數據是否以正確的格式進入 – Tinku 2010-02-23 12:25:23

1

使用$.getJSON而不是$。員額。
這將返回一個對象(解析JSON),而不是字符串

+0

使用GET方法,但pradeep正在POST數據。 – 2010-02-23 17:53:17