Javascript JS在隐藏未定义的值后仅从数组返回第一个id的值
我从一个页面和一个php服务器端pageserver.php发送了许多动态post id,用这些id进行查询,以查找mysql中新添加的数据 如果在mysql中没有发现任何新添加的数据,则返回一个未定义的值。所以我添加了这个如果msg.id!==未定义&msg.detail!==未定义&&msg.name!==未定义{//do here}以隐藏未定义的 但是在添加了上面的行之后,我的脚本很好地隐藏了未定义的值,但只返回第一个CID的新添加值 这意味着如果CID发送ids100、101、102、103等。对于php,它只返回100个id的新添加值并附加它 请问哪里有问题 注:如果未找到新数据,则返回所有CID值,但也返回未定义的值 我的javascript:Javascript JS在隐藏未定义的值后仅从数组返回第一个id的值,javascript,php,jquery,Javascript,Php,Jquery,我从一个页面和一个php服务器端pageserver.php发送了许多动态post id,用这些id进行查询,以查找mysql中新添加的数据 如果在mysql中没有发现任何新添加的数据,则返回一个未定义的值。所以我添加了这个如果msg.id!==未定义&msg.detail!==未定义&&msg.name!==未定义{//do here}以隐藏未定义的 但是在添加了上面的行之后,我的脚本很好地隐藏了未定义的值,但只返回第一个CID的新添加值 这意味着如果CID发送ids100、101、102、1
var CID = []; // Get all dynamic ids of posts (works well)
$('div[data-post-id]').each(function(i){
CID[i] = $(this).data('post-id');
});
function addrep(type, msg){
CID.forEach(function(id){
if (msg.id !== undefined && msg.detail !== undefined && msg.name !== undefined) {
$("#newreply"+id).append("<div class='"+ type +""+ msg.id +"'><ul><div class='newkochi'>"+ msg.name +"</div><div class='cdomment_text'>"+ msg.detail +"</ul></div>");
}
});
}
function waitForRep(){
$.ajax({
type: "GET",
url: "server.php",
cache: false,
data: {CID : CID},
timeout:15000,
success: function(data){
addrep("postreply", data);
setTimeout(waitForRep, 15000 );
},
error: function(XMLHttpRequest, textStatus, errorThrown){
setTimeout(waitForRep, 15000); }
});
}
$(document).ready(function(){
waitForRep();
});
server.php
while (true) {
if($_GET['CID']){ //cid got all dynamic post id as: 1,2,3,4 etc.
foreach($_GET['CID'] as $key => $value){
$datetime = date('Y-m-d H:i:s', strtotime('-15 second'));
$res = mysqli_query($dbh,"SELECT * FROM reply WHERE qazi_id=".$_GET['tutid']." AND date >= '$datetime' ORDER BY id DESC LIMIT 1") or die(mysqli_error($dbh));
$data = array();
while($rows = mysqli_fetch_assoc($res)){
$data[]=$rows;
$data['id'] = $rows['id'];
$data['qazi_id'] = $rows['qazi_id'];
$data['username'] = $rows['username'];
$data['description'] = $rows['description'];
$data['date'] = $rows['date'];
//etc. all
$id = $rows['id'];
$qazi_id = $rows['qazi_id'];
$username = $rows['username'];
$description = $rows['description'];
//etc. all
} //while close
} //foreach close
$name .='<p class="name">'.$username.' Says:</p>';
$detail .=''.$description.'';
$data['name'] = $name;
$data['detail'] = $detail;
// do others something more like as above
if (!empty($data)) {
echo json_encode($data);
flush();
exit(0);
}
} //request close
sleep(5);
} //while close
发现javascript中的问题:
var CID = []; // Get all dynamic ids of posts (works well)
$('div[data-post-id]').each(function(i){
CID[i] = $(this).data('post-id');
});
function addrep(type, msg){
CID.forEach(function(id){
if (msg.id !== undefined && msg.detail !== undefined && msg.name !== undefined) {
$("#newreply"+id).append("<div class='"+ type +""+ msg.id +"'><ul><div class='newkochi'>"+ msg.name +"</div><div class='cdomment_text'>"+ msg.detail +"</ul></div>");
}
});
}
function waitForRep(){
$.ajax({
type: "GET",
url: "server.php",
cache: false,
data: {CID : CID},
timeout:15000,
success: function(data){
addrep("postreply", data);
setTimeout(waitForRep, 15000 );
},
error: function(XMLHttpRequest, textStatus, errorThrown){
setTimeout(waitForRep, 15000); }
});
}
$(document).ready(function(){
waitForRep();
});
修正1:
var CID = [$('div[data-post-id]').length];
$('div[data-post-id]').each(function(i){
CID[i] = $(this).data('post-id');
});
修正2:
var CID = [];
$('div[data-post-id]').each(function(i){
CID.push($(this).data('post-id'));
});
插入问题:
function addrep(type, msg){
CID.forEach(function(id){
//You are inserting the comment in all html tags havin id ='newreplay' + id
//Here you need to correct you logic like: if (msg.id == id) //do something
if (msg.id !== undefined && msg.detail !== undefined && msg.name !== undefined) {
$("#newreply"+id).append("<div class='"+ type +""+ msg.id +"'><ul><div class='newkochi'>"+ msg.name +"</div><div class='cdomment_text'>"+ msg.detail +"</ul></div>");
}
});
}
SQL字符串中的限制为1,因此查询只获取一行。您应该在查询字符串中转义请求参数,以避免SQL injectionsOk remove请求,但限制1不是导致此情况的任何因素,但也会将其删除而不起作用。我的php文件获得了我上面提到的所有ID,我不考虑注释显示在哪里。这将是我的下一个问题。我当前的脚本获取并返回了除添加上述JS行之外的所有内容。但JS行添加了“未定义”值。谢谢你。