Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript JSON/PHP

  • This topic is empty.
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #188012
    dtek516
    Participant

    Need to retrieve values from ajax object, server-side and I’ve spent days trying to figure this out. Ajax/JS Newbie newbie.

    Here’s my js:

    question1 = {testNumber : 20, Answer: 3, timeComplete: 00:05:37}
    question2 = {etc, etc, etc}

    allQuestions = {q1:question1, q2:question2, q3:question3, q4:question4, q5:question5, q6:question6};

                        $.ajax({
    
                             type: 'POST',
    
                             url: allURL, 
    
                             data: {"questions" : JSON.stringify(allQuestions)},
    
    
                             success: function( resp ){
    
                                $('#allanswers').append( resp );
    
    
                             },
                             error: function( xhr, status, errorThrown ) {
                                alert( "Sorry, there was a problem!" );
                                console.log( "Error: " + errorThrown );
                                console.log( "Status: " + status );
                                console.dir( xhr );
                            },
    
                        });     
    

    my php:

    <?php
    if (isset($_POST[“questions”])) {
    $questions = $_POST[“questions”];

        // Decode our JSON into PHP objects we can use
        $allQuestions = json_encode( $questions, true);
    
    }
    

    ?>

    when I print_f, I get:

    “{\\”q1\\”:{\\”testNumber\\”:\\”31\\”,\\”Answer\\”:\\”1\\”,\\”timeComplete\\”:\\”00:05:49\\”},\\”q2\\”:{\\”testNumber\\”:\\”30\\”,\\”Answer\\”:\\”2\\”,\\”timeComplete\\”:\\”00:05:75\\”},\\”q3\\”:{\\”testNumber\\”:\\”29\\”,\\”Answer\\”:\\”2\\”,\\”timeComplete\\”:\\”00:05:43\\”},\\”q4\\”:{\\”testNumber\\”:\\”28\\”,\\”Answer\\”:\\”2\\”,\\”timeComplete\\”:\\”00:05:43\\”},\\”q5\\”:{\\”testNumber\\”:\\”27\\”,\\”Answer\\”:\\”2\\”,\\”timeComplete\\”:\\”00:05:29\\”},\\”q6\\”:{\\”testNumber\\”:\\”27\\”,\\”Answer\\”:\\”2\\”,\\”timeComplete\\”:\\”00:05:67\\”}}”

    I’m so frustrated and spinning my wheels, and my boss is pissed. Can someone please point me in the right direction?

    #188013
    __
    Participant
    $allQuestions = json_encode( $questions, true);
    

    You’re encoding the already-encoded JSON, so now, it’s double-encoded. That’s exactly what you see when you print it.

    …perhaps you mean to use json_decode?

    #188016
    dtek516
    Participant

    Oops, I think that just got switched in my struggle. Lol. Thanks for pointing it out.

    How do I access those array values? I tried echo $ allQuestions [0][0];

    Also, echo $ allQuestions [0][‘testNumber’];
    as an example.

    Still nothing. I’m sorry to sound so desperate..but I’ve searched and switched, over and over.

    #188018
    __
    Participant

    echo $allQuestions[0]['testNumber']; as an example.

    Looking at your code above, I would not expect $allQuestions[0]['testNumber'] to exist. You seem to be using a “q{number}” naming scheme (i.e., $allQuestions['q1']['testNumber']).

    If you’re not sure about the structure of your array, inspect it:

    print "<pre>";
    var_dump( $allQuestions );
    
    #188019
    dtek516
    Participant

    I do, i believe my issue to be that i combined the objects, and now it’s a bit too deep.

    so, the original objects being:

    $question1{testNumber, answer, timeComplete};

    question2{ etc…}

    were combined into $allQuestions{ q1:question1, q2:question2….}
    so that i could send it off to my php, but now im having trouble accessing them.

    Believe me, I’m all about learning new things on my own, however, this seems like I’m just missing something and could really use the answer.

    As I had mentioned, i set up my ajax as POST, and my object as

    data: {“questions” : JSON.stringify(allQuestions)}

    because of my notation, am i accessing these array items in the same notation after the decode? such as $passedQuestions[q1][answer]? so something to that effect?

    #188020
    __
    Participant

    because of my notation, am i accessing these array items in the same notation after the decode? such as $passedQuestions[q1][answer]? so something to that effect?

    See my last comment.
    Judging by the code you’ve posted, I would expect to find $allQuestions['q1']['testNumber'], et.al..

    More importantly,

    re-read my comment about using var_dump.

Viewing 6 posts - 1 through 6 (of 6 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.