无需按f5即可重新加载聊天内容

时间:2017-02-23 12:49:07

标签: javascript php html

我想用PHP创建一个小聊天。一切正常,但聊天不会自动重新加载。有没有能解决我问题的功能?

我的PHP代码:

<?php
  $newmsg= mysqli_query($db_chat, "SELECT msg, username, time FROM messages");
  while($row = mysqli_fetch_object($newmsg))
  {
      echo '<div class="messagesinchat">';
        if ($row->username == "admin") {
          echo '<div class="ownmsg">';
          echo $row->username. ":&nbsp " . $row->msg;
          echo "<br />";
          echo '</div>';
        }
        else{
          echo $row->username. ":&nbsp" . $row->msg;
          echo "<br />";
        }
      echo '</div>';
  }
    ?>

2 个答案:

答案 0 :(得分:0)

你可以用PHP做到这一点:

header("Refresh:0");

请参阅Peter Mortensen关于堆栈溢出here

的答案

答案 1 :(得分:0)

您需要在显示页面中定义div,并且可以使用jQuery的.load()使用PHP脚本将聊天消息加载到div。然后,每隔x秒,重新加载div

假设你有:
display.php实际上,好吧......显示你的聊天记录。 (<div id='messagesinchat&gt;应该在这里。

display.php中,让jQuery像:

$(function() {
    setInterval(function() {
        $('#messagesinchat').load('chat.php?action=show');
    }, 1000);
});

chat.php?action=show中执行查询以搜索和打印您的消息。

相关问题