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

标签 javascript php ajax

我用 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'
    });
}

引用文献:

关于javascript - 一段时间后自动刷新某些部分,无需在 php 中再次加载页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33941011/

相关文章:

PHP在上传到Azure存储后获取Blob url

php - 当执行 "php artisan migrate"时,我得到 : Base table or view already exists: 1050 Table 'users' . (我正在使用 PHPSTORM 2018.3.4)

javascript - 发送到 PHP 时接收 Javascript 变量时出现问题

javascript - 在附加到正文之前获取 div 的大小

php - 如何使用ajax将空值传递给PHP?

javascript - 找到隐藏元素的 "potential"宽度

javascript - JQuery - 如果 ID 相同,则仅显示一个,并在几周内添加它

javascript - 单击按钮时替换文本

php - 如何从 PHP 中的 SSL 证书文件中获取到期日期

javascript - Treepanel节点行索引