Need Help With Json Array Print

Discussion in 'jQuery' started by minhazikram, Jan 20, 2013.

  1. #1
    I have a db query and a ajax script. my db query echo's out an array using json_encode and using ajax i am successful in catching that array but cannot print out the array materials. every time i attempt to print out the array objectOBJECT gets printed. Please help me.

    Code snippets given below

    This is my php code which echo the array

    $sub_array_cat = array();
    $id = $_POST['id'];
    $check_if_sub_cat_exist = "SELECT sub_cat_name FROM sub_cat_table WHERE main_id='{$id}'";
    $check_if_sub_cat_exist_query = mysql_query($check_if_sub_cat_exist,$connect);
    if(!$check_if_sub_cat_exist_query){die('error '.mysql_error());}
    if(mysql_num_rows($check_if_sub_cat_exist_query)>0)
    {
    while($out_d = mysql_fetch_assoc($check_if_sub_cat_exist_query))
    {
    $sub_array_cat[] = $out_d;
    }
    echo json_encode($sub_array_cat);

    exit();
    }//if(mysql_num_rows($check_if_sub_cat_exist_query)>0)
    else{
    echo "not found";
    exit();
    }

    And this is my ajax script

    function check_sub_cat(c){
    var x = c;
    $.ajax({
    type: "POST",
    url: "userpanel.php",
    dataType:"JSON",
    data: { id: c }
    }).done(function(jjj)
    {

    var h = jjj
    var lngth_of_array = jjj.length;
    alert(h) // this gives out [object,Object]

    });
    }

    I want to print the data. help needed
     
    minhazikram, Jan 20, 2013 IP
  2. dixcoder

    dixcoder Member

    Messages:
    71
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    26
    #2
    i hope this will help your problem
    $.ajax({
    type: "POST",
    url: "userpanel.php",
    dataType:"JSON",
    data: { id: c }
    }).done(function(jjj)
    {

    //var h = jjj
    var lngth_of_array = jjj.length;
    for(i=0;i<lngth_of_array;i++)
    {
    var h=jjj;
    alert(h.YOUR_FIELD_NAME);
    }
    //alert(h) // this gives out [object,Object]

    });
     
    dixcoder, Jan 24, 2013 IP
  3. tiamak

    tiamak Active Member

    Messages:
    81
    Likes Received:
    2
    Best Answers:
    3
    Trophy Points:
    58
    #3
    i guess this is because u use mysql_fetch_assoc. Try mysql_fetch_row.
    When u encode associative array to json then u will get object in javascript.
     
    tiamak, Jan 25, 2013 IP