2010-02-23 3 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); JQuery와

그러나 같은 존재하는 .each 루프 문에 필요한 fomat입니다 제대로

{"Patient_id":"R00006","Name":"admin","Age":"12","Weight":"67","Gest_age":"2"} 등이 줄을 인쇄 : alert(key + ': ' + value);는 Patient_id처럼 인쇄되지 않습니다 : R00006 모두는 0:{ 1:P 2:a 3:t 4:i 같은 인쇄 나누었다 .. 무엇이 문제일까요?

+0

가장 적합한 답을 수락하는 것을 잊지 마십시오. –

답변

2

을 반환은 $.each() 방법은 자바 스크립트 배열과 (길이 속성이) 배열과 같은 객체를 통해 반복입니다. PHP의 연관 배열 (keyword-> value)은 기본 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

좋은 점은 불필요한 루프를 없애는 것이 가장 좋습니다 –

+0

$ ("# patient_id") .change (function() { $ .post ( "/diabetes/patient_detail_change.php", { 위치 : $ ("# 위치") .Val(), 의사 : $ ("# doctor") .val(), patient_id : $ ("# patient_id"). val() }, 함수 (json_data) { 경고 (json_data); 알림 (json_data.Name); if (json_data.Age) {$ ("# age")} val (json_data.Age);} if (json_data.Name) {$ ("# name" (json_data.Weight) {$ ("# ropweight") .val (json_data.가중치);} if (json_data.Gest_age) {$ ("# gest_age")} val (json_data.Gest_age);} }, "Json" ); }}); – Hacker

+0

나는 경고를 출력한다. (json_data); { "Patient_id": "R00006", "Name": "admin", "Age": "12", "Weight": "67", "Gest_age": "2"}와 비슷하지만 경고 (json_data.Name); 정의되지 않은 것으로 인쇄됩니다 :( – Hacker

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"); 

이렇게.

반환 된 데이터는 문자열로 처리되므로 각 문은 각 문자를 출력합니다.

당신은 $.post ("/diabetes/patient_detail_change.php",{ location:$("#location").val(),doctor:$("#doctor").val(),patient_id:$("#patient_id").val()}, function (json_data){ //blah blah },"Json");

대신 $ .post의 마지막 인수 "JSON"

+0

+1, 약 3 초 만에 나를 때려 눕힌다. ( – karim79

+0

나의 열망은 버그를 남겨 뒀을지도 모른다. ... –

1

정의를 참조하십시오.
이 객체 (JSON을 구문 분석)보다는 마 엘렌의 대답에 추가 문자열

+0

나는 그것을했고, 끝에서 json를 더했다. 그러나 이전과 같은 방법으로 인쇄했다. ( – Hacker

+1

오픈 방화범 goto Net -> xhr 탭에서 json 데이터가 올바른 형식인지 확인하십시오. – Tinku

1

사용 $.getJSON을 확인하시기 바랍니다 사용해야 여기 jQuery post definition

+0

GET 메서드를 사용하지만 pradeep이 데이터를 POST하는 중입니다 –