一段时间后自动刷新一些部分而不再在php中加载页面

时间:2015-11-26 14:23:42

标签: 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
}
?>

3 个答案:

答案 0 :(得分:1)

试试这个

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)

答案 1 :(得分:0)

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

参考文献:

答案 2 :(得分:0)

您没有指定当前的设置,因此通常会这样。

首先,检查服务器端if the request is made with 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.