简体   繁体   中英

Refreshing php page without reloading content?

I've searched everywhere for a method where a page can reload automatically for every x seconds without actually reloading the page's contents, I have php code(includes some html) which updates my database table whenever a new user joins the page. However this only works once.

PHP CODE

 <?php
                  $userOn5 = "SELECT * FROM `usersOn` WHERE name = '$username'";
$query4 = mysql_query($userOn5) or die (mysql_error()); 
 while ($row = mysql_fetch_array($query4)) { 
    // Gather all $row values into local variables for easier usage in output 
    $timenow = $row["time"];  
} 
                  $user = "SELECT * FROM `user`";
$query3 = mysql_query($user) or die (mysql_error()); 
 while ($row = mysql_fetch_array($query3)) { 
    $usernow = $row["name"];  
} 
$secs = time() - $timenow;

mysql_query("UPDATE user SET name = '$user'");
if ($username == $usernow) {
                  ?>
                    <div id="container" style="display:none;">

I've attempted using meta tag but that reloads the entire content, I've attempted moving the entire php code to a separate php file and tried loading it inside a div called 'show' in the page:

var auto_refresh = setInterval(
        function ()
        {
        $('#show').load('registeruser.php').fadeIn("slow");
        }, 10000); // autorefresh the content of the div after
                   //every 10000 milliseconds(10sec)

Basically what I'm trying to do is rerun the php code every 10 seconds. Help?

Quite likely your issue is due to browser cache. You can use the following workaround -- that's actually what ajax does when you use cache: false :

$(function() {
    var auto_refresh = setInterval(function () {
        var t = Date.now();
        $('#show').load('registeruser.php?t=' + t);
        $('#container').fadeIn("slow");
    }, 10000);
});

UPDATE

Alternatively, you can use the following:

$(function() {
    var auto_refresh = setInterval(function () {
        var t = Date.now();
        $('#show').load('registeruser.php?t=' + t).find('> div').fadeIn("slow");
    }, 10000);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM