Thread: php/JSON/jQuery

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2010
    Posts
    72
    Rep Power
    8

    php/JSON/jQuery


    Hi folks,

    I just can't get the hang of the php/json/jquery procedure, and despite having literally spent days trying to get it right ... I'm still not getting it.

    The more I try, the more I realise that I need help.

    I'm looking to echo/return data that is composed of a couple of arrays.

    OK, so this much I understand.
    1) my js page sends a POST to my php page, which runs a query (or more than one), and send the data in an echo/return (encoded with json_encode).
    2) my js page uses json_parse to divide the data into name/value pairs (or some similar magic) in order that I can then use the values as appropriate, such as displaying them using jquery.

    However, in my various attempts to get the procedure correct, I have come to realise that I don't even know what I should be seeing when I use the js alert function to display the data after it has been converted using json_parse -
    1) should it be displaying the actual contents of the arrays ?
    2) should it be displaying the name of the each object followed "Array" (rather than the contents of each array).
    3) should it be displaying "object" for each object, and "Object" for each array ?
    I've been getting all of the above, because (as I've said, I'm lacking the basic knowledge).

    So can someone please help me ?

    Presuming that someone can, here's some sample arrays

    PHP Code:
    $birthyeararray = array('birthyearvalue'=>'1''birthyearlanguage'=>'Select');
    $birthyeararray = array('birthyearvalue'=>'2017''birthyearlanguage'=>'2017');
    $birthyeararray = array('birthyearvalue'=>'2016''birthyearlanguage'=>'2016');
    $birthyeararray = array('birthyearvalue'=>'2015''birthyearlanguage'=>'2015');
    $birthyearjson '{"Birthyear":"'.$birthyeararray.'"}';

    $genderarray = array('gendervalue'=>'1''genderlanguage'=>'Select');
    $genderarray = array('gendervalue'=>'M''genderlanguage'=>'Male');
    $genderarray = array('gendervalue'=>'F''genderlanguage'=>'Female');
    $genderjson '{"Gender":"'.$genderarray.'"}';

    $populatearray = array('birthyear'=>'2017''gender'=>'M');
    $populatejson '{"Populate":"'.$populatearray.'"}';

    $loginarray = array('accountid'=>'12345''logindatetime'=>'20170125182022');
    $loginjson '{"Login":"'.$loginarray.'"}'

    $echo = array($birthyearjson,$genderjson,$populatejson,$loginjson);

    echo 
    json_encode($echo); 
    Code:
    var jsondata = JSON.parse(data);
    alert(data);
    alert(jsondata);
    When subjected to json_parse, my page alerts {"Birthyear":"Array"},{"Gender":"Array"},{"Populate":"Array"},{"Login":"Array"} - but there are no commas between the closing/opening curly brackets when displayed on the page via jQuery.

    As far as I can make out from reading examples on the web, I should be able (for example), to create a variable and populate it from any of the json output - for example, the "birthyearlanguage" of the second item in the Birthyear array :
    myjsonvar = jsondata["Birthyear"][1]["birthyearlanguage"];
    but nothing is alerted/displayed when I try to do so, so presumably I'm not populating the variable correctly.

    Alternately, instead of displaying {"Birthyear":"Array"},{"Gender":"Array"},{"Populate":"Array"},{"Login":"Array"}, I can get it to alert the actual array contents by just doing this echo - echo ($birthyeararray); - but when subjected to json_parse it alerts simply [object Object],[object Object],[object Object],[object Object] and won't write into a div via jQuery (presumably because it is still composed of objects rather than inate values).

    So could someone please give me chapter & verse of where I'm going wrong, and thereby, how to do it right ?
  2. #2
  3. Lazy Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,436
    Rep Power
    9645
    Don't do that $birthyearjson and $genderjson and whatnot stuff. json_encode() will encode everything for you, so if you need to add "Birthyear" keys or whatever then do that in a regular PHP array.

    You're also overwriting those variables with new values each time. A variable can only hold one thing at a time, so
    PHP Code:
    $var 123;
    $var 456;
    echo 
    $var// 456 
    And the "Array" in the output is because $birthyeararray is, well, an array, and PHP doesn't know what you're trying to do when you concatenate (.) it with another string and just uses "Array" as the value it adds.
    PHP Code:
    $array = array(123);
    $var "<" $array ">"// <Array> because the $array must be converted to a string
    echo $array// Array - because the conversion happens with output too 
    You've got a lot of data in that code, but it's all written right there in the code (as opposed to coming from a form or the database). Stick it all into a giant array, though you could use temporary variables (eg, $birthyeararray) if you really wanted.
    PHP Code:
    $echo = array(
        
    'Birthyear' => array(
            array(
    'birthyearvalue'=>'1''birthyearlanguage'=>'Select'),
            array(
    'birthyearvalue'=>'2017''birthyearlanguage'=>'2017'),
            array(
    'birthyearvalue'=>'2016''birthyearlanguage'=>'2016'),
            array(
    'birthyearvalue'=>'2015''birthyearlanguage'=>'2015')
        ),
        
    'Gender' => array(
            array(
    'gendervalue'=>'1''genderlanguage'=>'Select'),
            array(
    'gendervalue'=>'M''genderlanguage'=>'Male'),
            array(
    'gendervalue'=>'F''genderlanguage'=>'Female')
        ),
        
    'Populate' => array('birthyear'=>'2017''gender'=>'M'),
        
    'Login' => array('accountid'=>'12345''logindatetime'=>'20170125182022')
    );
    echo 
    json_encode($echo); 

    Comments on this post

    • foreverforever agrees : Many thanks.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2010
    Posts
    72
    Rep Power
    8
    Thank you requinix,

    Firstly, I have no idea how I missed off the square brackets [] after each array name whilst populating them. They are in my own code but somehow I missed them all out whilst writing my post. I have no idea how it happened but it did - possibly a good indicator of how confused my brain is over this issue.

    I used test data to fill the arrays, which would ordinarily be populated by multiple rows of results extracted from my database, so I had to retain my separate arrays (birthyear, gender, populate, login) and modify/abbreviate your suggested echo as follows, but it worked marvellously.

    PHP Code:
    $echo = array('Birthyear' => $birthyeararray,'Gender' => $genderarray,'Populate' => $populatearray,'Login' => $loginarray); 
    Thank you so much.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2010
    Posts
    72
    Rep Power
    8
    In case it helps anyone who may be as confused as I was :

    presuming that your php page is correctly echoing the data -

    a javascript alert should display the data, separated by a series of curly brackets {} and square brackets [].

    a javascript alert of the data after it has been subjected to json_parse should display [object Object].
    EDIT : see post by requinix (below, post#5) - about using console.log to view the data instead of wondering what [objectObject] actually means.

    to cause javascript to alert the value of a specific item in an array, for example the "birthyearlanguage" of the first record in the Birthyear array (see post#1 in this thread for details) :
    Code:
    alert(jsondata["Birthyear"][0]["birthyearlanguage"]);
    The alert will display "Select".
    Last edited by foreverforever; January 27th, 2017 at 04:02 PM.
  8. #5
  9. Lazy Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,436
    Rep Power
    9645
    You should learn about console.log.
    Code:
    console.log(jsondata);
    Do that, make sure it executes, then pull up your browser's developer tools (or whatever it calls it... try F12) and look for the Javascript console. You should see a dump of the entire jsondata value - no more "[object Object]" and guessing what it is.

    Comments on this post

    • foreverforever agrees : Thank you for the further advice.

IMN logo majestic logo threadwatch logo seochat tools logo