重新加载PHP包括没有innerHTML

时间:2014-11-02 19:30:18

标签: php dynamic refresh chat

当我的Minecraft服务器上有人发送聊天消息时,插件会将该消息发送到listener.php文件,该文件将信息输入到Mysql数据库中。我正在尝试在我为网站构建的管理面板中实时显示它(尽可能接近)。虽然使用.load可以在页面上获取内容,但它会破坏我在聊天框中使用的jquery操作的滚动条,使其无法向下滚动浏览帖子。这是因为它必须有足够的值才能保证在滚动条出现之前需要滚动,但是使用.load时,唯一存在的是chat.php的内容被加载到。我可以让聊天工作的唯一方法是将chat.php与php包括在一起,其中chat.php看起来像:

<?php


echo "<div class='block messages scrollBox'><div class='scroll' style='height: 320px;'> ";

$con=mysqli_connect("localhost","bduser","pass","dbase");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT * FROM server_chat ORDER BY date DESC , time DESC LIMIT 15");

while($row = mysqli_fetch_array($result)) {

$user = $row['user'];
$message = $row['message'];
$date = $row['date'];
$time = $row['time'];


echo "<div class='item clearfix'><div class='image'><a href='#'><img src='../face.php?u=$user'         class='img-polaroid' /></a></div><div class='info'><a class='name' href='#'>$user</a><p>$message</p>    <span>$time $date</span></div></div>";



}

mysqli_close($con);

echo "</div></div>";
?>

如何保留上述格式以在我的数据库中显示聊天表的内容,但是每隔5秒左右更新一次?有没有办法自动重新查询数据库?虽然代码片段有帮助,但我在学习过程中,要么通过q链接指向正确的方向,要么使用什么方法,要么告诉我需要做什么并将编码留给我,从长远来看会有更多帮助。提前谢谢!

1 个答案:

答案 0 :(得分:0)

注意:复制该PHP代码,我改变它

试试这个:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>
<script>
            $(document).ready(function() {
                var interval = setInterval(function() {
                    $.ajax({
                        url: 'chat_reload.php',
                        success: function(data) {
                            $('#toreload').html(data);
                        }
                    });
                }, 1000);
            });        
</script>



<?php


echo "<div class='block messages scrollBox'><div class='scroll' style='height: 320px;'><div id='toreload'> ";

$con=mysqli_connect("localhost","bduser","pass","dbase");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT * FROM server_chat ORDER BY date DESC , time DESC LIMIT 15");

while($row = mysqli_fetch_array($result)) {

$user = $row['user'];
$message = $row['message'];
$date = $row['date'];
$time = $row['time'];


echo "<div class='item clearfix'><div class='image'><a href='#'><img src='../face.php?u=$user'         class='img-polaroid' /></a></div><div class='info'><a class='name' href='#'>$user</a><p>$message</p>    <span>$time $date</span></div></div>";



}

mysqli_close($con);

echo "</div></div></div>";
?>

和: 在文件夹中,这个PHP代码在哪里创建名为 chat_reload.php 的新文件,并将此代码放在那里:

<?php
    $con=mysqli_connect("localhost","bduser","pass","dbase");
    // Check connection
    if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    $result = mysqli_query($con,"SELECT * FROM server_chat ORDER BY date DESC , time DESC LIMIT 15");

    while($row = mysqli_fetch_array($result)) {

    $user = $row['user'];
    $message = $row['message'];
    $date = $row['date'];
    $time = $row['time'];


    echo "<div class='item clearfix'><div class='image'><a href='#'><img src='../face.php?u=$user'         class='img-polaroid' /></a></div><div class='info'><a class='name' href='#'>$user</a><p>$message</p>    <span>$time $date</span></div></div>";



    }

    mysqli_close($con);
?>