如何附加一个id's的详细信息只通过php或jquery提供一次
How to append one id's detail only once by php or jquery
我想从sql only once
中追加新添加的数据。
所以,我使用了下面的脚本来显示sql中新添加的数据,但它会一个接一个地附加相同的数据。
这意味着,如果我在sql中添加test by thisuser
,它会连续追加4或5次。
如何制作,一个id的描述只显示一次。
我的Jquery:
function addmsg(type, msg){
$("#messages").append(
"<div class='msg "+ type +"'>"+ msg.description +" by "+ msg.username +"</div>"
);
}
function waitForMsg(){
$.ajax({
type: "GET",
url: "server.php",
async: true,
cache: false,
timeout:15000,
success: function(data){
addmsg("new", data);
setTimeout(
waitForMsg,
1000
);
},
error: function(XMLHttpRequest, textStatus, errorThrown){
setTimeout(
waitForMsg,
15000);
}
});
};
$(document).ready(function(){
waitForMsg();
});
server.php
global $dbh;
header('Content-Type: application/json; charset=utf-8');
while (true) {
date_default_timezone_set('Asia/Dhaka');
$datetime = date('Y-m-d H:i:s', strtotime('-15 second'));
$results = mysqli_query($dbh,"SELECT * FROM comments WHERE qazi_id='$id' AND date >= '$datetime' ORDER BY date DESC LIMIT 1") or die(mysqli_error($dbh));
$rows = mysqli_fetch_assoc($results);
$row[] = array_map('utf8_encode', $rows);
$data = array();
$data['id'] = $rows['id']; /* I want use this id to display once*/
$data['likes'] = $rows['likes'];
$data['username'] = $rows['username'];
$data['img'] = $rows['img'];
$data['description'] = $rows['description'];
$data['parent_id'] = $rows['parent_id'];
$data['date'] = $rows['date'];
//has data
if (!empty($data)) {
echo json_encode($data);
flush();
exit(0);
}
sleep(5);
}
尝试删除async: true,
并进行检查。还有,为什么在成功部分再次调用waitformsg函数?setTimeout( waitForMsg, 1000 );
相关文章:
- PHP/JQUERY-显示json的每个子数组
- 如何避免试图用php+jquery显示php起始页的无限循环
- PHP Jquery选项卡:内容加载到一个选项卡's面板
- onClick按钮不工作javascript/php/jquery mobile
- URL PHP/jQuery的链接预览
- 使用 php/jquery 动态更新单个模态
- PHP jQuery使用多个单词传递值
- PHP/JQUERY定义包含的页面
- PHP/Jquery-在PHP函数中呼应Jquery
- PHP-Jquery-简单模式的开放数据
- (php/jquery)根据另一个选中的复选框值隐藏和显示复选框
- 添加<tr>元素到动态表,动态不刷新页面,php-jquery
- 意外的PHP/jQuery/JSON交互差异
- jqgrid子网格row_id空格不起作用PHP jquery
- 如何使用PHP jquery和ajax从MySQL中检索多个值
- 在单击PHP / Jquery时更新附加的变量
- PHP JQuery 对话框和数据表 + 丢失布局
- 背景图像旋转器.php + jQuery每4秒显示一次随机照片
- php/jquery buffer full ?页面未完全加载
- 使用 ajax 拖放功能对列字段重新排序?PHP/jQuery.