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']
)));
Content-type
for json isapplication/json
; so it should look likeheader('Content-type: application/json');
JSON_NUMBERIC_CHECK
, or all PHP numbers will be converted to JavaScript string types. If the cols you're querying contain no numbers, you won't have to worry, but you should do it since it's a good habit.