Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am having trouble accessing json values return from an ajax call.

Here is the call:

    $.ajax({
    type: 'POST',
    data: 'user_id=' + user.user_id + '&user_login='+ user.user_login,
    //dataType: 'json',
    url: 'http://###/user-stats.php',
    success: function(data){
        alert(data);
        //alert(data.games_won[0]['winner_id']);
        //console.log(data);
        if(data) {
            $('#hand-stats-display').html(data);
            //$.mobile.changePage( "#user-home", { transition: "fade"} );
        }else{ 
            $('.messages').html("No stats available at this time... Please try again later.");
            //alert(data);
        }

    },
    error: function(){
        //console.log(data);
        alert('Ajax Login Request ✗');
    }
    });

Here is the PHP:

$myrows['hands_won'] = $wpdb->get_results( "SELECT * FROM wp_winners WHERE user_id=" . $user_id );


if ( $myrows ) 
{
    //echo('Rows Exist ✓');
    //echo "List of {$meta_key1} posts, sorted by {$meta_key2}";
    foreach ( $myrows as $id ) 
    {
        $creds[]=$id;
        //echo('MyRows: ' . $myrows[id]);
    }
}

//echo "Winner ID:" .$myrows[0]->win_id;
//echo "Winner 2 ID:" .$myrows[1]->win_id;



echo json_encode($creds);

It returns a json object like:

[{"win_id":"1","user_id":"1","game_id":"1"},{"win_id":"2","user_id":"1","game_id":"2"}]

I can not seem to access these with the javascript I am using. Any advice is appreciated.

share|improve this question
    
What happens if you use jQuery() instead of $()? –  toscho May 12 '12 at 17:47
add comment

1 Answer 1

up vote 0 down vote accepted

Looks like you want to use something like result = $.parseJSON(data). Then you can access the data with result.win_id, for example.

As an aside, you should sanitize the input to that database query using prepare with something like:

$wpdb->get_results( $wpdb->prepare( "SELECT * FROM wp_winners WHERE user_id=%d", $user_id ) );

share|improve this answer
    
Still not working for me... It seems like result.win_id should be something like result[0].win_id, bc it is multidimensional. appreciate your time. –  auz1111 May 12 '12 at 17:07
    
Yea, you could also use jQuery's $.each to loop through them. When you say it's not working, what does that mean exactly? Can you try logging the result? result = $.parseJSON(data); console.log(result); –  Evan May 13 '12 at 15:54
    
Ok using $.each(data, function(i,item){ var win_id = '<h1>'+item.win_id+'</h1>'; alert(win_id); }); I can see that two rows are returned, which is correct. Unfortunately, the win_id value is still undefined. I just can't figure out the syntax for accessing the variable at this time. –  auz1111 May 13 '12 at 18:07
    
Isn't the syntax what you just described? Inside of the .each loop you can just call item.win_id, and presumable item.user_id and item.game_id as well. As for why it's undefined, that is very hard to say without seeing the rest of your code. Is the $wpdb result undefined? Are there any errors? Make sure you enable WP_DEBUG. If possible, maybe drop the rest of your code into a gist or something to help figure out what else might be the cause. –  Evan May 13 '12 at 21:11
    
Ok, so I fell victim to a few of my own errors and in the end the loop ultimately helped me out. I still can't figure out how to access them outside the loop, but at least I can dump the values out to something I can read. Thanks! –  auz1111 May 14 '12 at 2:43
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.