【问题标题】:JS return only 1st id's value from array after hide undefined value隐藏未定义值后,JS 仅从数组中返回第一个 id 的值
【发布时间】:2015-03-13 18:18:25
【问题描述】:

我从一个页面发送了许多动态帖子 ID,一个 php 服务器端页面(server.php)使用这些 id 进行查询以找出 mysql 中的新添加的数据

如果在 mysql 中未找到任何新添加的数据,则返回 undefined 值。所以我添加了这个if (msg.id !== undefined && msg.detail !== undefined && msg.name !== undefined) { //do here } 来隐藏未定义。

但是在添加了上述行之后,我的脚本很好地隐藏了undefined 值,但只返回第一个 CID 的新增值。

也就是说如果 CID 向 php 发送 ids(100, 101, 102, 103 等),它只返回 100 id 的新增值并附加它。

请问这里哪里有问题?

注意如果没有上述行,它会很好地返回所有 CID 的值,但如果没有找到新数据,也会返回 undefined 值。

我的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();
});

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

【问题讨论】:

  • 您的 SQL 字符串中有 LIMIT 1,因此查询只获取一行。您应该在查询字符串中转义 REQUEST 参数以避免 SQL 注入
  • OK 删除 REQUEST,但 LIMIT 1 不是任何因素,但也删除它并且不起作用。

标签: javascript php jquery


【解决方案1】:

在 javascript 中发现的问题:

修复 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>");
}
});
}

【讨论】:

  • 我的 php 文件得到了我上面已经提到的所有 id,而且我没有考虑评论显示在哪里。这将是我的下一个问题。除了我添加上面的 JS 行之外,我当前的脚本获取并返回了所有内容。但是为“未定义”值添加了 JS 行。谢谢你。
猜你喜欢
  • 2023-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-27
  • 1970-01-01
  • 2013-11-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多