2010-02-23 5 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; 
?> 

Ceci est mon code PHP.Problème de tableau jQuery

Ceci est le code 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); } 
    }); 
    }); 
}); 

alerte (json_data); cette ligne se imprime correctement comme

{"Patient_id":"R00006","Name":"admin","Age":"12","Weight":"67","Gest_age":"2"} qui est le fomat nécessaire pour jquery

Mais la déclaration de la boucle de .each qui est présente comme: alert(key + ': ' + value); n'imprime pas comme Patient_id: R00006 et tous .mais il imprime comme 0:{ 1:P 2:a 3:t 4:i .. quel pourrait être le problème?

+0

N'oubliez pas d'accepter la réponse qui vous convient le mieux. –

Répondre

2

En plus de la réponse de Matt Ellen, la méthode $.each() est pour le bouclage sur les tableaux JavaScript et des objets ressemblant à un tableau (qui ont une propriété de longueur). Les tableaux associatifs de PHP (mot-clé-> valeur) sont convertis en un objet JavaScript natif à la place. Vous pouvez utiliser une boucle à la place 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]);} 
} 

Mais vous n'avez pas besoin probablement la boucle. Vous pouvez simplement utiliser:

$.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

Bon point, il est préférable de se débarrasser des boucles inutiles –

+0

$ ("# patient_id") .Modification (function() { .post $ ( "/diabetes/patient_detail_change.php", { emplacement: $ ("# emplacement") val(), médecin:. $ ("# médecin") .val(), patient_id: $ ("# patient_id"). val() },Fonction(json_data) { alert (json_data); alert (json_data.Name); if (json_data.Name) {$ ("# nom") .val (json_data.Name);} if (json_données.Age) {$ ("# age"). Val (json_data.Age);} si (json_data.Weight) {$ ("# ropweight"). val (json_data.Weight);} if (json_data.Gest_age) {$ ("# gest_age") .val (json_data.Gest_age);} }, "Json" ); }); – Hacker

+0

Je l'ai fait son imprime toujours l'alerte (json_data); correctement comme {"Patient_id": "R00006", "Nom": "admin", "Age": "12", "Poids": "67", "Gest_age": "2"}} mais alert (json_data.Name) imprime comme indéfini :( – Hacker

2

Dans votre message, vous devez spécifier que vous renvoyez 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"); 

Comme ça. À l'heure actuelle, les données renvoyées sont traitées comme une chaîne, de sorte que chaque instruction sort chaque caractère.

Voir la définition ici jQuery post definition

+0

+1, me battre d'environ trois secondes :( – karim79

+0

Mon empressement pourrait avoir laissé des bugs ... –

1

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

s'il vous plaît vérifier le dernier argument "JSON"

+0

Je l'ai fait et ajouté json à la fin. Mais aussi son impression de la même manière que précédemment :( – Hacker

+1

Ouvrir firebug goto Net -> onglet xhr et vérifiez que les données json arrivent au bon format – Tinku

1

utilisation $.getJSON au lieu de .post $.
Ceci renverra un objet (analysable JSON), plutôt qu'une chaîne

+0

Cela utilise la méthode GET, mais pradeep est POSTing les données –