0
<?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;
?>

This is my PHP code.

This is the jQuery code.

$("#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); this line prints properly like

{"Patient_id":"R00006","Name":"admin","Age":"12","Weight":"67","Gest_age":"2"} which is the fomat required for jquery

But the .each loop statement which is present like: alert(key + ': ' + value); does not print like Patient_id : R00006 and all .but it prints like 0:{ 1:P 2:a 3:t 4:i ..what might be the problem?

0

4 Answers 4

2

In addition to Matt Ellen's answer, the $.each() method is for looping over JavaScript arrays and array-like objects (that have a length property). PHP's associative arrays (keyword->value) are converted into a native JavaScript object instead. You could use a for...in loop instead:

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]);} 
}

But you probably don't need the loop. You can just use:

$.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"
);
Sign up to request clarification or add additional context in comments.

3 Comments

$("#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); alert(json_data.Name); if (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.Weight);} if (json_data.Gest_age) { $("#gest_age").val(json_data.Gest_age);} }, "Json" ); });
I did this its still prints the alert(json_data); properly like {"Patient_id":"R00006","Name":"admin","Age":"12","Weight":"67","Gest_age":"2"} but alert(json_data.Name); prints as undefined :(
Hi pradeep. There is a very minor typo in Andy's code you need to change "Json" to "json".
2

In your post statment you need to specify that you are returning 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");

Like so.

At the moment your returned data is being treated as a string, so the each statement is outputting each character.

See the definition here jQuery post definition

Comments

1

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

please check the last argument "Json"

2 Comments

I did that and added json at the end .but then also its printing in same way as earlier :(
Open firebug goto Net -> xhr tab and check that json data is coming in proper format
1

Use $.getJSON instead of $.post.
This will return an object (parsed JSON), rather than a string

1 Comment

That uses the GET method, but pradeep is POSTing the data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.