0

我创建了一个聊天框,但是当我删除不需要的评论时出现问题。对于插入的每个评论,我都有一个与 onclick 事件关联的删除函数这是我显示评论的方式:(评论存储在数据库中)

var refreshMsg = setInterval(function() {
    $.post("chatFunc.php", { action: "load", id_userMsg: '<?php echo $id_user; ?>' },
function(dVLoadUsers) {
$("#divMsg").append(dVLoadUsers);   // appending the new comments into a div        $("#divMsg").scrollTop($("#divMsg")[0].scrollHeight);   
        }); 
}, 399); // Loop time in milliseconds

这是删除功能(jquery):

function DeleteMsg(id_msg){
$.post("chatFunc.php", { action: "del", id_msgChat: id_msg, id_userMsg: '<?php echo $id_user; ?>' },
    function(dVLoad) {
        $("#divMsg").html(dVLoad);
        $("#inputMsg").val("").focus();
    }); 
}

这是 php 中的操作:“del”:

if($action == "del"){
$msgDeleted = "Comment Deleted";
$qBorraMsj = mysqli_query($classDB->con,"UPDATE chat SET message = '$msgDeleted ' WHERE id LIKE '$id_msgChat'"); // Updating message content to "Comment Deleted"
$qChat = mysqli_query($classDB->con,"SELECT id, message, userNameFROM chat ORDER BY id ASC LIMIT 60"); // Getting the last 60 messages inserted
while($row = mysqli_fetch_array($qChat) ){
    if($row['message'] == $msgDeleted ) // Showing deleted messages
        echo '<span class="styleUsName">'.$row['userName'].'</span>:&nbsp;<span class="styleMsgDel">'.$row['message'].'</span></br>';
    else // Showing the rest of the messages
        echo '<span class="styleUsName">'.$row['userName'].'</span>:&nbsp;<span class="styleMsgDel">'.$row['message'].'</span>&nbsp;<span class="styleDelMsg" onClick="deleteMsg('.$row['id'].')">delete</span></br>';
 }
}

所以,基本上当一条消息被删除时,div 会重新加载并插入最后 60 条消息。它适用于删除消息的人,但不适用于所有人,因为只有他在执行代码。

如何刷新面板并为每个人提供消息?谢谢你的时间。

4

1 回答 1

2

Like Jon suggests, you could send the ID of the most recent message that has been displayed to the server and then only get messages that are newer than that. You would then .append() the new message/s rather than clearing out the whole div with .load().

于 2013-02-25T03:26:57.177 回答