Ryukotsusei Posted July 1, 2010 Share Posted July 1, 2010 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 More sharing options...
ChemicalBliss Posted July 1, 2010 Share Posted July 1, 2010 You would use cookies - or an easier option (using cookies) is Sessions. Have a look at some tutorials they are very simple. -cb- Link to comment https://forums.phpfreaks.com/topic/206397-auto-refresh-using-variable/#findComment-1079708 Share on other sites More sharing options...
Ryukotsusei Posted July 1, 2010 Author Share Posted July 1, 2010 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']; ?>"/> Link to comment https://forums.phpfreaks.com/topic/206397-auto-refresh-using-variable/#findComment-1079722 Share on other sites More sharing options...
ChemicalBliss Posted July 1, 2010 Share Posted July 1, 2010 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- Link to comment https://forums.phpfreaks.com/topic/206397-auto-refresh-using-variable/#findComment-1079730 Share on other sites More sharing options...
Ryukotsusei Posted July 1, 2010 Author Share Posted July 1, 2010 Thank you! Everything seems to be working now. One final question though: if I want to be able to stop the refreshing, how would I achieve this? I tried putting the meta tag in an if statement, but it wasn't liking it. Link to comment https://forums.phpfreaks.com/topic/206397-auto-refresh-using-variable/#findComment-1079745 Share on other sites More sharing options...
ChemicalBliss Posted July 1, 2010 Share Posted July 1, 2010 How do you mean wasnt liking it? It's best to provide as much information as possible, such as "why you want the refresh to stop", "example scenarios" etc etc. -cb- Link to comment https://forums.phpfreaks.com/topic/206397-auto-refresh-using-variable/#findComment-1079761 Share on other sites More sharing options...
Ryukotsusei Posted July 1, 2010 Author Share Posted July 1, 2010 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. Link to comment https://forums.phpfreaks.com/topic/206397-auto-refresh-using-variable/#findComment-1079765 Share on other sites More sharing options...
mrMarcus Posted July 1, 2010 Share Posted July 1, 2010 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'] .'"/>'; } } Link to comment https://forums.phpfreaks.com/topic/206397-auto-refresh-using-variable/#findComment-1079768 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.