Jump to content

Auto Refresh using Variable


Ryukotsusei

Recommended Posts

I've been looking everywhere but I haven't been able to find an answer to this.

 

I'm trying to create a page that will auto refresh every x seconds using meta tags. On this page I have a dropdown where I want people to be able to change the number of seconds the page refreshes. The default is every 60 seconds. If I try to change it to, say, 5 seconds, it works the first time. But when it refreshes again, it loses this data and goes back to the default of 60 seconds.

 

So what can I do to make it save this data so it continues to refresh at whatever rate the user chose?

Link to comment
https://forums.phpfreaks.com/topic/206397-auto-refresh-using-variable/
Share on other sites

There must be something else I'm doing wrong because using sessions isn't working either. Here's what I have so far:

 

session_start();
     $rate = $_POST['time'];
     $_SESSION['rate']=$rate;
     
     if($rate=="")
     {
       $_SESSION['rate']=60;
     }

 

And the meta tag:

 

<meta http-equiv="refresh" content="<?php echo $_SESSION['rate']; ?>"/>

Ah see you need to look at your code logically.

 

What your code is doing:

Starting the Session (this is fine)

Set variable $rate to whatever is in the $_POST['time'] variable (regardless of wether a form was submitted)

Set the session Variable to whatever is in the $rate variable (Again, regardless of whats in there/if the form ahs been submitted)

Then your checking if $rate is empty (which to be honest i would use empty() ).

 

This basically means that $rate will only not be empty if there has been a form submitted.

 

First (after you start the session), you need to check wether a form has been submitted.

This way you can only change the session variable once a form is submitted.

 

i would use "isset($_POST['time'])" as the condition to check wether to chagne the current session variable.

 

-cb-

Sorry.

 

Using this code:

 

<?php
if($_SESSION['rate']!=0)
{
echo "<meta http-equiv='refresh' content='$_SESSION['rate'];'>";
}
?>

 

I get this error:

 

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home1/ssuhatch/public_html/viewer.php  on line 79

 

Usually I can fix that by changing some punctuation, but in this case I just can't find the problem.

Something quick.  There's obviously more that needs to be added to it, but it's some theory:

 

<?php
if (!isset($_SESSION['rate'])) {
if (!isset($_SESSION['freeze'])) {
	echo '<meta http-equiv="refresh" content="60"/>';
}
}
else {
if (isset($_SESSION['rate') && ctype_digit($_SESSION['rate'])) {
	echo '<meta http-equiv="refresh" content="'. $_SESSION['rate'] .'"/>';
}
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.