Best way to autmatically refresh a page every 5 to 15 minutes without user knowing?

Hi there,

Is there away where i can refresh the page without the vistor using it for example i want to refresh the part of a function i guess you could call it refreshing the page but only for the displaying of function refresh();

the kind of function i am looking for is somthing like this.


REFRESH(page(),1000);

abit like javascripts set time out but is there one for PHP itself?

Thanks,William

Use AJAX calls on a setInterval() timer or something. SitePoint articles cover working with the XmlHttp object.

Cheers,
D.

can i get php to refresh the same page every 15 minutes in a loop with setinterval and javascript settimeout function?

i found this in a tutorial which i am playing around with but how can i get it to display somthnig every minute non-stop?


?>
<script type="text/javascript">
var c=0;
var t;
var timer_is_on=0;

function timedCount()
{
document.getElementById('output').value=c;
c=c+1;
t=setTimeout("timedCount()",6000);
}

function doTimer()
{
if (!timer_is_on)
  {
  timer_is_on=1;
  timedCount();
  }
}
</script>
<?

Is that possible??

PHP is on the server, so no. You can use js to refresh the page, and use php via ajax calls.

is there away to refresh the page using javascript every 15 minutes in a continius loop?

PHP aint gonna do it because once it leaves the server PHP loses control.
You can use setInterval and an Ajax call (Javascript) to get new content to replace the current page.

With jQuery:


function refresh_handler() {
    function refresh() {
       $.get('your-server-script.php', null, function(data, textStatus) {
           $("body").html(data);
        });
    }
    setInterval(refresh, 300*1000); //every 5 minutes
}

$(document).ready(refresh_handler);

jQuery isn’t my first language so that code might not be perfect, but should give you a starting point.
Replace your-server-script.php with the URL of your server side script. It should return the HTML that will become the content of your <body> tag.
If you just want to update part of the document change the selector from “body” to something more specific (e.g “#someElementID”)

Is JQuery abit like ajax?

also can you explain what does this part mean?

$(“body”).html(data);

is that where i want the data to be displayed??

jQuery is a framework for JavaScript. It includes functionality for making AJAX calls.

I really suggest you do a bit of Googling on the subjects. Don’t expect people on forums to spoon-feed you every bit of information you need.

Cheers,
D.

jQuery is a Javascript library that simplifies a lot of common JS programming tasks such as selecting elements in the DOM, handling events (click, mouseover), running Ajax transactions, doing animation etc.

Ajax is the process of getting data from the server to the browser or page without reloading the whole page. jQuery provides functions to manage Ajax transactions (as do all JS libraries).

$(“body”).html(data) means this:
$ is the jQuery object (shortcut).
“body” is a HTML selector (same as you might use in CSS). In this case it will get all <body> tags, of which there should be exactly 1.
data will be the response from your server side script (the data of the ajax transaction) and the .html() function call will set the innerHTML of <body> to that data.

That line of code will be called when the the Ajax transaction finishes, and it will set the content of <body> to be whatever your .php script displays.

Or maybe you can expect them to.

Cheers,
D.

definitely here at Sitepoint - not sure whether I always like the helpfulness of Sitepoint members, it makes people lazy, and if you don’t help as much as those regulars, you’re seen as unfriendly…

Luckily I don’t care about being perceived as unfriendly. But yes, the culture here does seem to make people entirely dependent on hand-outs which actually harms them rather than helps.

Cheers,
D.

why do i get this error in the error console of firefox?

Error: $ is not defined
Source File: http://localhost/timerefresh.html
Line: 11

Line 11 is

$(document).ready(refresh_handler);

why do i get that error?

Have you loaded the jquery library?

BTW localhost doesn’t work so well on the web :stuck_out_tongue:

no since i’ve never used it b4 is that why i am getting that error?

ive downloaded it and tried to include it into my page and this is what i have atm


<script type="text/javascript" src="jquery-1.3.2.min.js">
function refresh_handler() {
    function refresh() {
       $.get('time.php', null, function(data, textStatus) {
           $("body").html(data);
        });
    }
    setInterval(refresh, 300*1000); //every 5 minutes
}
$(document).ready(refresh_handler);
</script>

i have no errors been shown but nothing is being displayed so why do i get a blank page?

Thanks

You need to close the first script and open another
<script type=“text/javascript” src=“jquery-1.3.2.min.js”></script>
<script type= …>
//your code here
</script>

ive included it but i am still getting this as an error

Error: $ is not defined
Source File: http://localhost/timerefresh.html
Line: 11

My code is


<script type="text/javascript" src="jquery.js"></script> 
<script type="text/javascript">
function refresh_handler() {
    function refresh() {
       $.get('time.php', null, function(data, textStatus) {
           $("body").html(data);
        });
    }
    setInterval(refresh, 300*1000); //every 5 minutes
}
$(document).ready(refresh_handler);
</script>

Why don’t you just command the page to automatically refresh itself?

You can place a meta command in the head section of the page in order to do so.

15 x 60 = 900 seconds


<head>
    <meta http-equiv="refresh" content="900">
    ...
</head>