page remembers value on refresh

perkeyone

something clever
Reaction score
71
when i initially load this page it gives 2 alert messages. "0" and then "1"
on refresh it alerts "1" and then "2"...
why?
i am expecting it to be reset to 0 and 1 each time but it doesnt.
PHP:
<?php
echo "<input type=\"hidden\" value=0 id=\"selectcount\" name=\"selectcount\"/>";
echo "<script language=\"javascript\">alert(document.getElementById(\"selectcount\").value)</script>";
echo "<script language=\"javascript\">++document.getElementById(\"selectcount\").value</script>";
echo "<script language=\"javascript\">alert(document.getElementById(\"selectcount\").value)</script>";
?>

EDIT: this problem doesnt occur in ie but does in firefox
EDIT2: it seems to be a cache issue. i found that a "hard refresh" avoids the problem.
i am still looking for a way to fix this in my code though, since i didnt know how to hard refresh till about 3 min ago.
EDIT3: as a temporary fix, i am using javascript to manually set the input to 0 each time the page loads. id really like to find a true fix for this issue though.
 

perkeyone

something clever
Reaction score
71
no good.
i need to know how to prevent it from caching.
i did some google searches but found mostly information from a few years ago when firefox3 came out, so im not sure if any of it still applies to the most recent version.
 

Lyerae

I keep popping up on this site from time to time.
Reaction score
105
PHP:
<?php
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
?>

From the PHP manual.
 

perkeyone

something clever
Reaction score
71
i put the two lines lyerae gave in my index page before my include statements. now my page only seems to work the first time it loads. =/ im sure this is just some rule i forgot about how headers work... i know theres a certain time where you should and shouldnt use them because it messes with output or something... idk. anyways, what would be the best place to put those lines?

at least one of my functions broke after adding those lines. i have a js function which adds a select box to my form. the select box is labeled according to how many select boxes there are. (box 1, box 2, etc) after i added those headers, the select boxes are only added the first time the page loads. also, when i tried debugging i found that it still remembers how many boxes there were, which is the thing i was trying to prevent when i added those lines in the first place. =/ i think maybe i didnt put them in the right place. upon further inspection i found that the function was not working because the value is in the cache... the incorrect value is causing the function to try to access elements that dont exist.

here is the page i am using to test with.
i placed the two lines in the equivalent place.
the value is still remembered.
PHP:
<?php
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
include("entry_functions.php");
include("resourcelibrary/functions2.php");
rlConnect();
?>
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="styles.css"/>
<script type="text/javascript" src="entry.js"></script>
</head>
<body>
<?php
echo "<input type=\"hidden\" value=\"0\" id=\"selectcount\" name=\"selectcount\"/>";
//echo "<script language=\"javascript\">document.getElementById(\"selectcount\").value=0</script>";
echo "<script language=\"javascript\">alert(document.getElementById(\"selectcount\").value)</script>";
echo "<script language=\"javascript\">++document.getElementById(\"selectcount\").value</script>";
echo "<script language=\"javascript\">alert(document.getElementById(\"selectcount\").value)</script>";
?>
</body>
</html>
 

Slapshot136

Divide et impera
Reaction score
471
normal f5 is refresh, but if you do control or command + f5, then it should override the cache (in Firefox), does this work for you?
 

perkeyone

something clever
Reaction score
71
yeah i already know hard refresh will work.
but i cant expect other people who would use the page to only hard refresh.
 

celerisk

When Zerg floweth, life is good
Reaction score
62
Form elements should be inside a "form".
language="javascript" has been deprecated several years ago already.
You should set a "doctype".
You do not need php to output a static line of javascript...


Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Test</title>
<link rel="stylesheet" type="text/css" href="/css/style.css"/>
<script type="text/javascript">
function foo()
{
    document.getElementById("selectcount").value++;
    alert(document.getElementById("selectcount").value);
}
</script>
</head>
<body onload="foo()">
<form method="post" action="#" [B]autocomplete="off"[/B]>
<p>
<input type="hidden" value="0" id="selectcount" name="selectcount" />
</p>
</form>
</body>
</html>

Note the autocomplete="off", that should help.
And, yes, Firefox... the older it gets the more it becomes a PITA.
 

Magentix

if (OP.statement == false) postCount++;
Reaction score
107
What is it exactly that you are trying to achieve?

Can't really see as you edited the OP several times before I read this thread.
 

perkeyone

something clever
Reaction score
71
@celerisk
the code that i have posted is just my test page.
i made the test page so that i could experiment with different proposed solutions,
without the risk of causing further complications.
my real code is dynamic, but since i copy pasted from it to the test page, it's now out of context

turning autocomplete off solved my problem,
but is that the only way to fix this issue?
for some of my fields it might be useful to have autocompletion.

EDIT: i specified doctype and now my tables are a little messed up in firefox and significantly messed up in ie.
before i had it so they looked almost exactly the same. =[

@magentix
i have a hidden field in my form. some button calls a function which increments that hidden field.
if i refresh the page the field remains incremented.
my problem is, i want it to reset to the initial value if the user refreshes the page.
 

celerisk

When Zerg floweth, life is good
Reaction score
62
" the code that i have posted is just my test page. "

I guessed that much. There's just too many things missing :p


" but is that the only way to fix this issue? "

Probably, yes (other then getting a "better" FF).
You can put it on individual "input"s and remove it from the "form".


" i specified doctype and now my tables are a little messed up "

That is a perfect reason why you should always add one.
It means you found some problem.
Like an element that isn't closed but should or so.

And, well, you just never know what kind of browser your visitors are using.
As such, you should always present them valid code.
 

Magentix

if (OP.statement == false) postCount++;
Reaction score
107
Uhm, just set the field's value to zero using javascript...

With jQuery:
Code:
(function($){
    $(function(){
        $("#theinputbox").val(0);
    });
})(jQuery);

Edit: If you don't know jQuery, what it does is:
- pass the jQuery object to the local $-variable
- call the $(document).ready() function (shorthand is demonstrated here)
- set the field's value to zero

This will work on any refresh: hard or soft
 

perkeyone

something clever
Reaction score
71
@magentix
yeah i know,
ive been using javascript to reset it manually already from at least as early as post #9 of this thread, but not using jquery.
thanks though, your idea would work.
i just wanted to find some other ways.
 

celerisk

When Zerg floweth, life is good
Reaction score
62
" from at least as early as post #9 "

Well, as seen in post #1:
" EDIT3: as a temporary fix, i am using javascript to manually set the input to 0 each time the page loads. id really like to find a true fix for this issue though. "



" never seen that syntax "

It's for the paranoid that still want to use $ for jQuery even if the page already includes some other javascript library that remaps $.
Read the documentation (second example).

(function...)() is a function (obvious) that executes itself immediately once loaded (because of the () at the end).

Stupid example:
var five = (function(){ return 5; })();
Is equivalent to
var five = 5;

It has its uses at times :p
 

perkeyone

something clever
Reaction score
71
@celerisk
i edited the op several times, at least one of those edits happened after he posted, so i gave him the benefit of the doubt that it wasnt in the op when he read it. post 9 however has been there for a while, but it is very easy to not read a line that is commented out, especially since i didnt put any other comments in there to describe what was trying to accomplish. so i didnt mean to bust him out, i only meant to say that i figured that much out at least.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top