PHP変数を取得して入力値に挿入するjQueryを取得しようとしています。私はどこからでも解決策を探していました。jQueryはPHPスクリプトからJSONエンコードデータを受け取りません
ここにjQueryコードがあります。
$(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');
}
});
});
});
ここでPHP側です。
//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']
)));
ヘッダーをtext/jsonに設定しようとしましたか? PHPでエコーする前に、次のようにしてください:header( 'Content-type:text/json'); – kingcoyote
@kingcoyote jsonの適切な 'Content-type'は' application/json'です。 'header( 'Content-type:application/json');' – Edwin
'JSON_NUMBERIC_CHECK'を強制することや、すべてのPHPの数字がJavaScriptの文字列型に変換されることに注意してください。照会している列に数値が含まれていない場合は、心配する必要はありませんが、それは良い習慣であるため、行う必要があります。 – Edwin