一段时间后自动刷新某些部分,而无需在php中再次加载页面


refresh some part after some time automatically without loading the page again in php

我已经用PHP编写了代码。它显示了不同用户发布的帖子。我刚刚将该页面包含在另一个页面中,只是为了将其与其他元素一起查看。现在我希望我包含的部分必须在某个间隔后自动刷新。我知道它可以通过AJAX实现,但我对此了解不多。下面是post.php文件上的代码,我已经在主页上使用include"post.php";将其包含在主页中。我希望下面的代码输出在主页上每30秒刷新一次,但不重新加载页面

<?php
$fetchpost=mysqli_query($conn,"select * from post where b_id IN (select u_id from frndlist where b_id='$b_id')");
?><?php
while($fpost=mysqli_fetch_array($fetchpost)){
    $author_id=$fpost[0];
    $post_id=$fpost[1];
    $post_date=$fpost[2];
    $inb_namefetch=mysqli_query($conn,"select b_name from ub_per where b_id='$author_id'");/*getting author's name*/
        $inb_mailfetch=mysqli_query($conn,"select b_email from ub_per where b_id='$author_id'");/*getting author's email*/
    $inb_mailfetched=mysqli_fetch_array($inb_mailfetch,MYSQLI_ASSOC);
    foreach($inb_mailfetched as $authormail){
        $authormail;
    }
    $inb_profpicfetch=mysqli_query($conn,"select prof_pic from ub_per where b_id='$author_id'");/*getting author's profile picture*/
    $inb_namefetched=mysqli_fetch_array($inb_namefetch,MYSQLI_ASSOC);
    $inb_profpicfetched=mysqli_fetch_array($inb_profpicfetch,MYSQLI_ASSOC);
    foreach($inb_profpicfetched as $fetchedimage){
        $piccheck=$fetchedimage;
    }
    if(isset($piccheck)){
    $authorimage="http://localhost/uploads/".$authormail."/profilepics/".$fetchedimage;
;   }else{$authorimage="http://localhost/content/user.jpg";}
    foreach($inb_namefetched as $authorl){
        $author=ucwords($authorl);
    }
    if(isset($fpost[3])){
    $post_content=$fpost[3];
}
    if(isset($fpost[4])){
    $post_image=$fpost[3];
}
?><div class="postingdiv">
<table>
<tr class="authortr" ><td class="author"><a id="imlink" href="http://localhost/profile/view_profile.php?id=<?php echo $author_id; ?>"><img class="authorpic" src="<?php echo $authorimage;?>"></a><a id="tlink" href="http://localhost/profile/view_profile.php?id=<?php echo $author_id; ?>"><?php echo $author; ?></a></td></tr>
<tr class="posttr" ><td class="post"><?php if(isset($post_content)){echo $post_content;} if(isset($post_image)){echo "<br><img id='postimg' src='http://localhost/uploads/".$author_mail."/postpics/".$postimage."'>";} ?></td></tr>
</table></div>
<?php include"commenter.php";
?>
<div class="makoment"><form name="mkoment" method="post" action="http://localhost/content/comment/makecomment.php" ><input type="hidden" name="commentere" value="<?php echo $b_id;  ?>"><input type="hidden" name="postsid" value="<?php echo $post_id;  ?>">  <textarea class="mycommentarea" name="accomment" placeholder="make a comment"></textarea><br><input type="submit" class="commentbutton" value="Comment"></form></div>
<br>
<?php
}
?>

使用jQuery执行此操作的示例:

setInterval(updateFn, 1000 /* or however long you want the interval */);
function updateFn(){
    $.post("page.php", function(response){
        console.log(response); //handle the data pinged back by the PHP page in the variable 'response'
    });
}

参考文献:

  • jQuery Ajax
  • jQuery Post

像这样尝试

setInterval(function(){
    $.ajax({
        url:"path to php file",
        data:{key:value},//key value pairs
        type:"POST"
        success: function(data){
             alert(data);
        }
    });
},10000);
   ^ interval you want(now its 10 seconds)

您没有指定当前的设置,所以这是一般情况。

首先,在服务器端检查请求是否是用AJAX发出的,这样您就可以只发送页面的特定部分,或者使用不同的路由/页面/文件来处理请求。

if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
    echo 'Requested with AJAX';
}

然后在浏览器端,使用JavaScript每隔x秒发送一次GET请求。类似的东西

setTimeout(function() {
    var ajax = new XMLHttpRequest();
    ajax.onreadystatechange = function() {
        if (ajax.readyState == XMLHttpRequest.DONE) {
            if (ajax.status == 200) {
                document.getElementById('posts').innerHTML = ajax.responseText;
            }
        }
    }
    ajax.setRequestHeader('X-Requested-With', 'XMLHttpRequest'); // Let PHP know we're doing it with AJAX.
    ajax.open("GET", "/posts.php", true);
    ajax.send();
}, 5000); // 5000 milliseconds = 5 seconds.