views:

225

answers:

3

Is it possible to refresh only the part of the page? How?

the part:

if (checkExpiry($member->expires)==true) {
    print timeLeft($leftts);
} else {
    print "expired";
}

I have table which is showing name, email, time until membership ends and I need to refresh 'time until membership ends' every second.

+5  A: 

You can check out AJAX, where Javascript calls a PHP application of yours an updates only a certain part of the site.

Ólafur Waage
+1  A: 

I presume you mean "refresh the page". The answer is not directly. PHP is executed on the server and "normal" HTTP doesn't do server side pushes. This means that once a page is sent to a client, it won't reload unless something or someone on the client asks for it again.

To implement this, you're going to have to, as Ólafur suggested, use some Javascript on the client side to check the timer and then reload the page (or part of it if you prefer) using an AJAX style call.

Noufal Ibrahim
A: 

If you use Javascript, you can do like this:

<script src="jquery.js"></script>
<script>
$(document).ready(function()
{
   //time_to_expire.php is called every second to get time from server
   var refreshId = setInterval(function()
   {
     $('#expire').load('time_to_expire.php?userID=<? echo $userID; ?>);
   }, 1000);
});
</script>

inside time_to_expire.php you'll put your code to provide the time remaining to expire the user account!

Note that in that example I'm sending a variable "userID" that will contain the user ID for properly retrieve data from the time_to_expire.php file...

Hope it helps or give's you an idea... :)

Zuul
Can't get it working. expires.php?userid=x are showing the expiry correct, but the code: <script src="jquery.js"></script> <script> $(document).ready(function() { var refreshId = setInterval(function() { $('#expire').load('expires.php?userid=<?php echo $member->id; ?>); }, 1000); }); </script>doesn't work. Any ideas?
Tom
Many ideas, but can't you provide more about your code... or a working example... I'm working blind here.. :)btw:<script src="jquery.js"></script> -> It's the inclusion of the jquery library onto your page... must by inside header tags and must have the correct path to the jquery file.. go to www.jquery.com to download it...Only with that file the provided script will work..
Zuul