php スクリプトに json_encode がありますが、json_encode が while ループ内で呼び出されると、Jquery 側で失敗するようです。
ページ以外のブラウザー ウィンドウで結果を表示すると、実際に機能することがわかります。
例えば:
while ($row = mysql_fetch_array($result)) {
$id = $row['id'];
$title = $row['title'];
$content = $row['content'];
$data = array("id" => $id,
"title" => $title,
"success" => true
);
echo json_encode($data); // Inside the While Loop
}
ブラウザの出力は次のとおりです。
{"id":"15","title":"Advanced Booking Examples","success":true}
{"id":"14","title":"Advanced Booking Fields","success":true}
{"id":"11","title":"Add New Driver","success":true}
{"id":"10","title":"Registering a Driver \/ Add New Vehicle","success":true}
{"id":"8","title":"Dispatch Controls","success":true}
{"id":"7","title":"Troubleshooting Driver Device Checklist","success":true}
{"id":"6","title":"Requirements Checklist","success":true}
jQuery応答では、これは実際には空白ですがecho json_encode($data);
、whileループのすぐ外側を離れると、jQueryは応答を取得しますが、ループ内の最後のレコードである1レコードのみです。
ループからすべての結果を取得するには、jQuery が必要です。
JSはこちら
$.ajax({ // Send it off for prcessing
type: "POST",
dataType: 'json',
url: "includes/auto_suggest.php",
data: {
q: inputString
},
success: function (result) {
if (result.success) {
var id = result.id;
var title = result.title;
$('#auto_id').append("<input type='text' class='hideid' id='hideid_" + id + "' value='" + id + "'/>");
$('#auto_title').prepend("<p>" + title + "</p>");
}
}
});
何か案は?
前もって感謝します