473,397 Members | 1,961 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

jquery array

pradeepjain
563 512MB
hi guys ,

Expand|Select|Wrap|Line Numbers
  1. $("#patient_id").change(function(){
  2. $.post("/diabetes/patient_detail_change.php",{ location:$("#location").val(),doctor:$("#doctor").val(),patient_id:$("#patient_id").val()} ,function(data){
  3. alert(data);
  4. //$("#patient_id").html(data);
  5. });
  6.     });
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.
Feb 19 '10 #1
19 3735
pradeepjain
563 512MB
i outputted the array its like

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

Expand|Select|Wrap|Line Numbers
  1. $.each(data, function(key, value) { 
  2. alert(key + ': ' + value); 
  3. });
  4.  
but its not working . Is anything wrong?
Feb 19 '10 #2
Dormilich
8,658 Expert Mod 8TB
the only thing on my mind … debug by hand using Firebug and setting breakpoints.
Feb 19 '10 #3
pradeepjain
563 512MB
when i am printing it ,its printing like 0 :P 1:a 2:t .its taking each letter as an array element.
Feb 21 '10 #4
pradeepjain
563 512MB
no solution to this prob?
Feb 22 '10 #5
zorgi
431 Expert 256MB
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?
Feb 22 '10 #6
pradeepjain
563 512MB
i am getting the array in jquey in format
{"Patient_id":"R00020","Name":"admin","Age":"12 "," Weight":"67","Gest_age":"2"}

is this the rite format?
Feb 22 '10 #7
zorgi
431 Expert 256MB
Expand|Select|Wrap|Line Numbers
  1. var my_var = {"Patient_id":"R00020","Name":"admin","Age":"12"," Weight":"67","Gest_age":"2"}
  2.  
Yes you could do it that way
Feb 22 '10 #8
zorgi
431 Expert 256MB
You might want to have a look here JSON
Feb 22 '10 #9
pradeepjain
563 512MB
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.
Feb 22 '10 #10
zorgi
431 Expert 256MB
If you want to iterate through JSON
Expand|Select|Wrap|Line Numbers
  1. var my_json = {"Patient_id":"R00020","Name":"admin","Age":"12"," Weight":"67","Gest_age":"2"};
  2. $.each(my_json, function(i){alert(this)})
  3.  
Or if you want to iterate through form elements:

Expand|Select|Wrap|Line Numbers
  1. $(function(){
  2.            $("form > *").each(function(){alert(this.name)})
  3.            })
  4.  
Feb 22 '10 #11
pradeepjain
563 512MB
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.
Feb 23 '10 #12
pradeepjain
563 512MB
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

Expand|Select|Wrap|Line Numbers
  1. $("#patient_id").change(function(){
  2. $.post("/diabetes/patient_detail_change.php",{ location:$("#location").val(),doctor:$("#doctor").val(),patient_id:$("#patient_id").val()} ,function(data){
  3. alert(data);
  4.  
  5.  var my_json = {"Patient_id":"R00020","Name":"admin","Age":"12"," Weight":"67","Gest_age":"2"};
  6. //alert(my_json);
  7. $.each(my_json, function(i){alert(this)});
patient_detail_change.php

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. include_once('db.php');
  3. $location = $_POST['location'];
  4. $doctor = $_POST['doctor'];
  5. $patient_id = $_POST['patient_id'];
  6. if(($location != "") && ($doctor != "")){
  7. $sql = "select Name,Age,Gest_age,Weight from rop_form where Location = '".$location."' and Doctor = '".$doctor."' and Patient_id = '".$patient_id."'";
  8. $result = mysql_query($sql);
  9. $myresult  = "";
  10. while($row = mysql_fetch_array($result))
  11.         {
  12.         $myresult['Patient_id'] = "R".$patient_id;
  13.         $myresult['Name'] = $row['Name'];
  14.         $myresult['Age'] = $row['Age'];
  15.         $myresult['Weight'] = $row['Weight'];
  16.         $myresult['Gest_age'] = $row['Gest_age'];
  17.         $myresult = json_encode($myresult);
  18.         }
  19. }
  20. else
  21. {
  22. $myresult .= "";
  23. }
  24. echo $myresult;
  25. ?>
  26.  
Feb 23 '10 #13
pradeepjain
563 512MB
hope ma code is clear enough!
Feb 23 '10 #14
zorgi
431 Expert 256MB
Expand|Select|Wrap|Line Numbers
  1. $.post("/diabetes/patient_detail_change.php", function(json_data){alert(json_data);})
  2.  
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.
Feb 23 '10 #15
pradeepjain
563 512MB
nope man it did not work :(

see the code
jquery code:

Expand|Select|Wrap|Line Numbers
  1. $("#patient_id").change(function(){
  2. $.post("/diabetes/patient_detail_change.php",{ location:$("#location").val(),doctor:$("#doctor").val(),patient_id:$("#patient_id").val()} ,function(json_data){
  3.  
  4. alert(json_data);
  5.  
  6. $.each(json_data, function(key,value){
  7. alert(key + ': ' + value);
  8. if(key == 'Name'){ $("#name").val(value);}
  9. if(key == 'Age'){ $("#age").val(value);}
  10. if(key == 'Weight'){ $("#ropweight").val(value);}
  11. if(key == 'Gest_age'){ $("#gest_age").val(value);}
  12.  
  13. });
  14.  
  15. });
  16.     });
the alert(json_data); line prints this

{"Patient_id":"R00008","Name":"","Age":"12","Weigh t":"67","Gest_age":"2"}

but the alert(key + ': ' + value); is printing like
0:{ 1:P 2:a and so on .

the php code is like this and as u pointed out i have corrected the variable and array issue
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. include_once('db.php');
  3. $location = $_POST['location'];
  4. $doctor = $_POST['doctor'];
  5. $patient_id = $_POST['patient_id'];
  6. if(($location != "") && ($doctor != "")){
  7. $sql = "select Name,Age,Gest_age,Weight from rop_form where Location = '".$location."' and Doctor = '".$doctor."' and Patient_id = '".$patient_id."'";
  8. $result = mysql_query($sql);
  9. $myresult  = "";
  10. while($row = mysql_fetch_array($result))
  11.         {
  12.         $myresult1['Patient_id'] = 'R'.$patient_id;
  13.         $myresult1['Name'] = $row['Name'];
  14.         $myresult1['Age'] = $row['Age'];
  15.         $myresult1['Weight'] = $row['Weight'];
  16.         $myresult1['Gest_age'] = $row['Gest_age'];
  17.         }
  18.         $myresult = json_encode($myresult1);
  19. }
  20. else
  21. {
  22. $myresult .= "";
  23. }
  24. echo $myresult;
  25. ?>
Feb 23 '10 #16
zorgi
431 Expert 256MB
Expand|Select|Wrap|Line Numbers
  1. $.getJSON("/diabetes/patient_detail_change.php", function(json_data){ $.each(json_data, function(i){alert(i + ":" + this)})})
  2.  
Hope this works
Feb 23 '10 #17
pradeepjain
563 512MB
nope.its not printing anything at all now after adding getJSON
Feb 23 '10 #18
zorgi
431 Expert 256MB
$_GET instead of $_POST.

More info about getJSON here
Feb 23 '10 #19
pradeepjain
563 512MB
hii i had to use something like

Expand|Select|Wrap|Line Numbers
  1. $("#patient_id").change(function(){
  2. $("#name").val('');
  3. $("#Age").val('');
  4. $("#ropweight").val('');
  5. $("#Gest_age").val('');
  6.  
  7. $.getJSON (
  8.   "/diabetes/patient_detail_change.php",
  9.   { 
  10.     location:$("#location").val(),
  11.     doctor:$("#doctor").val(),
  12.     patient_id:$("#patient_id").val()
  13.   }, 
  14.  
  15.   function (json_data){
  16.  
  17.     if (json_data.Name) { $("#name").val(json_data.Name);}
  18.     if (json_data.Age) { $("#age").val(json_data.Age);}
  19.     if (json_data.Weight) { $("#ropweight").val(json_data.Weight);}
  20.     if (json_data.Gest_age) { $("#gest_age").val(json_data.Gest_age);}
  21.   }, 
  22.   "Json"
  23. );
  24. });
and $_GET in the php file. :).thanks for all the help!
Feb 24 '10 #20

Sign in to post your reply or Sign up for a free account.

Similar topics

83
by: liketofindoutwhy | last post by:
I am learning more and more Prototype and Script.aculo.us and got the Bungee book... and wonder if I should get some books on jQuery (jQuery in Action, and Learning jQuery) and start learning about...
26
by: RobG | last post by:
Do some of the regulars here need to re-think their (sometimes strident) opposition to libraries? Both Microsoft and Nokia have announced support for jQuery. It seems to have gained quite a bit...
53
by: souporpower | last post by:
Hello All I am trying to activate a link using Jquery. Here is my code; <html> <head> <script type="text/javascript" src="../../resources/js/ jquery-1.2.6.js"</script> <script...
5
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums...
0
by: amskape | last post by:
hi Friends, I need to Upload some files in a Listing , by clicking corresponding Upload link, then a popup window will come with Browse option as Shown in attachment File. My actual need is...
4
Tj Jurado
by: Tj Jurado | last post by:
Hi guys, I am new to this and need a little help. I have an html block to be rotated and placed in an array via php as well as printed as an xml format then. Without refreshing the page, the...
0
by: BaseballGraphs | last post by:
Hello, I am trying to format the output of my data with the autocomplete function from jQuery. So far, I've been able to build the entire process, however, I want to limit the information I show...
0
by: BaseballGraphs | last post by:
Hello, I am making use of jQuery's .autocomplete plugin, and I would appreciate some assistance formating the results that show up in the dropdown menu that populates when text is written into the...
0
by: empiresolutions | last post by:
I'm building a navigation that is built heavily on jQuery. I have it working as needed except that when mousing over links to quickly it spikes the processor and the navigation hangs. By hangs i mean...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.