php / ajax / mysql重新加载div而不重新加载整页

时间:2016-04-11 19:45:08

标签: php jquery ajax

我在重新加载我的div时遇到了问题,我尝试了不同的方法,但它不起作用。 我想每3秒重新加载一次div = invidual所以当一个人每隔3秒发布一次帖子显示帖子时, 我的代码就是这个

<div id="reload">
    <div class="msg wall">

    <?php 

        $u_id = base64_decode($_GET['u_id']);

            $clave = $conn->prepare("SELECT * FROM friends WHERE user1='$user_id' OR user2='$user_id'");
            $clave->execute();
            $llave = $clave->fetch(PDO::FETCH_BOTH);

                $numero = $llave['hash'];
                $usuario_2 = $llave['user2'];
                $usuario_1 = $llave['user1'];



                $nur = $conn->prepare("SELECT * FROM messages WHERE group_hash='$u_id'");
                $nur->execute();
                while($row_pos = $nur->fetch(PDO::FETCH_BOTH)){

                    $msg = $row_pos['msg_content'];
                    $from = $row_pos['writer'];
                    $fecha = $row_pos['msg_date'];


                $user = $conn->prepare("SELECT * FROM user WHERE user_id='$from'");
                $user->execute();
                $row_user = $user->fetch(PDO::FETCH_BOTH);

                    $name = $row_user['user_name'];
                    $user_image = $row_user['user_image'];
                    $user_key = $row_user['user_id'];


                    ?>



                    <div id="individual">
                            <span><?php echo $name;?></span><span id="izq"><?php echo $fecha;?></span>
                            <p><?php echo $msg;?></p>   
                    </div>  

            <?php 

                    }

            ?>

        </div>
</div>

1 个答案:

答案 0 :(得分:0)

要重新加载div的数据,您必须将PHP脚本放入单独的文件中。然后,在jquery上调用php脚本,请求最新消息并更新它,如下所示:(这是jquery)

//function to make the function repeat every 3 seconds
setInterval(updateDiv, 3000);

//Function to make the magic happen
function updateDiv(){
    $.get("urlTo.phpFile.php", function(data){
      $('#individual').html(data);
    });
}

在php文件中,您只想显示其中包含span的{​​{1}}和p,而不是周围的echo。< / p>