AJAX聊天——用户(命令)的个人提醒


AJAX chat - Personal alerts for users (commands)

所以我有一个ajax聊天我一直在编程,和一个命令系统。

基本上,我希望系统给用户一个警告,如果他没有使用任何命令,只发布一个斜杠('/'),并且只能由发布它的用户查看。

但我不确定如何使它工作。这是我的加载消息方法,在聊天中:

    public function loadMessages($username)
    {
        $this->fetch = $this->pdo->prepare("SELECT * FROM messages ORDER BY date, time ASC LIMIT 30");
        $this->fetch->execute();
        while ($row = $this->fetch->fetch(PDO::FETCH_ASSOC))
        {
            if ($row['isAlert'] == '1')
            {
                echo '<b>'.$row['date'].', '.$row['time'].' ['.$row['username'].']: </b><font color="red">'.$row['message'].'</font> <br />';
            }
            else
            {
                echo '<b>'.$row['date'].', '.$row['time'].' ['.$row['username'].']: </b>'.$row['message'].' <br />';
            }
        }
    }

你可以在while循环中看到第一个if statement,看看消息是否为全局警告,如果是,将其涂成红色。

但是,我如何才能使消息只能被警报用户查看?

任何想法?

在页面上有一个隐藏的div,然后使用JavaScript检查命令是否有效,如果不显示div

例如:

 <div id="invalidChatCommand" style="display:none">
       <p>
          The command you entered is invalid.
       </p>
 <div>
<script type="text/javascript">
    var chatCommand = document.getElementById("command").value;
    if (chatCommand.length == 1 && chatCommand.substring(0, 1) == "/") { 
       // invalid input detected, show the div
       var invalidCommand = document.getElementById("invalidChatCommand");
       invalidCommand.style.display = "block";
    } else {
       // Send command to the server
    }
</script>