在第一頁上我有這樣的功能:jQuery的AJAX JSON問題
<script>
function update() {
$("#notice_div").html('Loading..');
$.ajax({
type: 'GET',
dataType: 'json',
data: latestid,
url: '2includejson.php?lastid='+ latestid + '',
timeout: 4000,
success: function(data) {
$("#cont_div").html(data);
$("#cont_div").clone().prependTo($("#newdiv"));
$("#notice_div").html('');
$("#cont_div").html('');
window.setTimeout(update, 4000);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
$("#notice_div").html('Timeout contacting server..');
window.setTimeout(update, 60000);
}
});
}
$(document).ready(function() {
update();
});
</script>
而且一些PHP。
所包含的文件:
<?
header("Content-Type: application/json", true);
$la = $_GET['lastid'];
include ("../../setup.php");
$jsonArray[] = array();
$count = 1; // first message is the newest on load
$get = $DB->query("SELECT * FROM board WHERE id>'$la' ORDER BY id DESC LIMIT 5", __FILE__, __LINE__);
while ($msg = $DB->fetch_array($get))
{
if($count == 1){
$latestid = $msg['id']; // newest message - this I need to pass to the other page
}
$count++;
$jsonArray = "$msg[msg]";
}
echo json_encode($jsonArray);
?>
我只是想學習如何使用AJAX和jQuery。
正如你看到的,我通過latestid爲JS變量通過URL 網址: '?2includejson.php lastid =' + latestid + '',
我是否需要更新/包後從一個較新的值包括頁面,但我不知道如何去做。在使用json之前,我可以用javascript覆蓋它,但現在我不知道...新的值將會以最新的形式再次發佈。
'成功:函數(數據){'< - 這裏的id在data.id,如果它從服務器傳遞 – Esailija
小心SQL注入在這裏:$ la = $ _GET ['lastid'] ; – BiAiB