tags:

views:

55

answers:

1

Hi guys, I am trying to access a jQuery JSON object, and I can't see anything wrong witj my code, but all I get when I try to access the object is undefined, I was thinking that maybe it's because of the way it is nested, but I am unsure, here is my code,

 function systems(dom_id, id){
  $.getJSON("get_systems.php", {uid:id}, function(data){
  // here I get the general system info related to the user 
     $.each(data, function(i, json){
    //here I get each systems name, related to each system
    $.getJSON('get_system_name.php', {uid:json.product_id}, function(data){
     console.log(data.products);
     $(dom_id).append('<tr><th>'+data.products+'</th></tr>');
    });

   });

  });
 };

The column name with the name of the system in it, is named products, hence me trying to get the name using data.products, but all I get is undefined as I mentioned above.

But when I just log data it shows the object, which looks like this, [Object { products="this is the product name"}] and this is the PHP that I use to get the system names,

get_system_name.php:

<?php
$uid = $_GET['uid'];

mysql_connect("localhost", "user", "1234") or die(mysql_error());
mysql_select_db("the_DB") or die(mysql_error());
$query = mysql_query("SELECT products FROM products WHERE product_id='$uid'") or die(mysql_error());

while($array = mysql_fetch_array($query, true)) {
 $string[] = $array;
}

$json = json_encode($string);
echo $json;
?>

Thanx in advance!

A: 

By the looks of this line: [Object { products="this is the product name"}]

data is actually an array of JSON, not a JSON. So if you did

data[0].products

, it should work. You might want to look into why it's coming back as an array though, unless that's your desired behaviour.

EMMERICH
Will do, thanx.
Got it working! It was pushing each JSON string into an array, which was not needed since I was only getting one name each time, so I changed the code to just return a string instead of an array with JSON strings in it, and now it works fine! Thanx alot!
Well done. I'd of helped further, but I don't know much php, so say anything other than I knew it was being pushed into an array.
EMMERICH