jquery array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pradeepjain
    Contributor
    • Jul 2007
    • 563

    jquery array

    hi guys ,

    Code:
    $("#patient_id").change(function(){
    $.post("/diabetes/patient_detail_change.php",{ location:$("#location").val(),doctor:$("#doctor").val(),patient_id:$("#patient_id").val()} ,function(data){
    alert(data);
    //$("#patient_id").html(data);
    });
        });
    i have written a code like this. It returns a array of value like Name => 'pradeep',Age=> '23'
    i.e data is array

    there are form fields like Name and Age. I need to take the values from the data and set it to each form fields how to do this.how to deal with arrays in jquery.
  • pradeepjain
    Contributor
    • Jul 2007
    • 563

    #2
    i outputted the array its like

    {"Patient_id":" R00020","Name": "admin","Age":" 12","Weight":"6 7","Gest_age":" 2"}
    i tried to iterate through it by using this code

    Code:
    $.each(data, function(key, value) { 
    alert(key + ': ' + value); 
    });
    but its not working . Is anything wrong?

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #3
      the only thing on my mind … debug by hand using Firebug and setting breakpoints.

      Comment

      • pradeepjain
        Contributor
        • Jul 2007
        • 563

        #4
        when i am printing it ,its printing like 0 :P 1:a 2:t .its taking each letter as an array element.

        Comment

        • pradeepjain
          Contributor
          • Jul 2007
          • 563

          #5
          no solution to this prob?

          Comment

          • zorgi
            Recognized Expert Contributor
            • Mar 2008
            • 431

            #6
            Ok I am trying to understand what you need. Basicly you have a form and you have array. You want to populate your form with values from this array? If so can you copy/paste your form?

            Comment

            • pradeepjain
              Contributor
              • Jul 2007
              • 563

              #7
              i am getting the array in jquey in format
              {"Patient_id":" R00020","Name": "admin","Age":" 12"," Weight":"67","G est_age":"2"}

              is this the rite format?

              Comment

              • zorgi
                Recognized Expert Contributor
                • Mar 2008
                • 431

                #8
                Code:
                var my_var = {"Patient_id":"R00020","Name":"admin","Age":"12"," Weight":"67","Gest_age":"2"}
                Yes you could do it that way

                Comment

                • zorgi
                  Recognized Expert Contributor
                  • Mar 2008
                  • 431

                  #9
                  You might want to have a look here JSON

                  Comment

                  • pradeepjain
                    Contributor
                    • Jul 2007
                    • 563

                    #10
                    i use php for server side scripting . i use json to convert php array to jquery required format array and sent it back to jquery and tried to iterate but the iteration is not happening.

                    Comment

                    • zorgi
                      Recognized Expert Contributor
                      • Mar 2008
                      • 431

                      #11
                      If you want to iterate through JSON
                      Code:
                      var my_json = {"Patient_id":"R00020","Name":"admin","Age":"12"," Weight":"67","Gest_age":"2"};
                      $.each(my_json, function(i){alert(this)})
                      Or if you want to iterate through form elements:

                      Code:
                      $(function(){
                      		   $("form > *").each(function(){alert(this.name)})
                      		   })

                      Comment

                      • pradeepjain
                        Contributor
                        • Jul 2007
                        • 563

                        #12
                        i will give this a try and let u know.

                        each of this key values is a form element like,Patient_id ,Name,Age. how do i take these values from array and give the form elements these values.

                        Comment

                        • pradeepjain
                          Contributor
                          • Jul 2007
                          • 563

                          #13
                          i am posting the code for help

                          here the jquery is printing properly if my_json array is passed but if i pass data which i returned from php code its not printing properly

                          Code:
                          $("#patient_id").change(function(){
                          $.post("/diabetes/patient_detail_change.php",{ location:$("#location").val(),doctor:$("#doctor").val(),patient_id:$("#patient_id").val()} ,function(data){
                          alert(data);
                          
                           var my_json = {"Patient_id":"R00020","Name":"admin","Age":"12"," Weight":"67","Gest_age":"2"};
                          //alert(my_json);
                          $.each(my_json, function(i){alert(this)});
                          patient_detail_ change.php

                          Code:
                          <?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))
                                  {
                                  $myresult['Patient_id'] = "R".$patient_id;
                                  $myresult['Name'] = $row['Name'];
                                  $myresult['Age'] = $row['Age'];
                                  $myresult['Weight'] = $row['Weight'];
                                  $myresult['Gest_age'] = $row['Gest_age'];
                                  $myresult = json_encode($myresult);
                                  }
                          }
                          else
                          {
                          $myresult .= "";
                          }
                          echo $myresult;
                          ?>

                          Comment

                          • pradeepjain
                            Contributor
                            • Jul 2007
                            • 563

                            #14
                            hope ma code is clear enough!

                            Comment

                            • zorgi
                              Recognized Expert Contributor
                              • Mar 2008
                              • 431

                              #15
                              Code:
                              $.post("/diabetes/patient_detail_change.php", function(json_data){alert(json_data);})
                              That should clear it up.

                              I didnt check your patient_detail_ change.php at all but glanced over. You have variable $myresult that keeps changing type from string to array back to string. Even though that php is loosely typed language and will allow you to do things like that you should be avoiding it. Imagine you have 5000 lines of code and your variables keeps changing type like that. It would be very difficult to understand or to read code like that.

                              Comment

                              Working...