user1269386
user1269386

Reputation: 9

jQuery not receiving JSON encoded data from PHP script

I'm trying to get jQuery to retrieve PHP variables and insert them into input values. I was looking everywhere trying to find a solution.

Here's jQuery code.

$(document).ready() {
$(function() {
    $.ajax({
      type: 'POST',
      url: 'loadstuff.php',
      data: {
            'something': something, 
            'different': different, 
            'another': another
            },
      dataType: 'json',
      success: function(data) {
        $('input[name=get_seomething_here]').val( 'something' );
        $('input[name=get_different_here]').val( 'different' );
        $('input[name=get_another_here]').val( 'another' );
}
    });

});
});

And here's PHP side.

//connecting to db etc.

$query = "SELECT something, different, another FROM stuff WHERE id='1'";  
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array( $result );

echo(json_encode(array('something' => $row['something'], 
'different' => $row['different'], 
'another' => $row['another']
)));

Upvotes: 0

Views: 519

Answers (2)

Smit
Smit

Reputation: 1569

aletzo is right.. but before doing that you have to do like:

data = eval("(" + data + ")");

Then you can use data.something, data.different and data.another

Upvotes: -1

aletzo
aletzo

Reputation: 2481

I think it should be:

$('input[name=get_seomething_here]').val( data.something );
$('input[name=get_different_here]').val( data.different );
$('input[name=get_another_here]').val( data.another );

Upvotes: 2

Related Questions