Refresh problem and posts

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I have a problem with refresh.

I have a contact form that sends an email when the submit button is
pressed. I have a hidden variable and the code for emailing is only
executed if the variable is set. The variable is cleared at the end of
the code.  All works as expected unless the REFRESH button is pressed
by the user in which case the script is executed and the emails are
sent once more. Debugging shows the test variable is set again even
though it was unset before exiting on the previous run. How can this

I know there are some things I could do like using database entries or
cookies to hold the variable or even jump to another page which I do
not want to do.  

There must be a simple explanation and hopefully solution to this.



Re: Refresh problem and posts

John wrote:
Quoted text here. Click to load it

the browser actually saves all of those POST variables...  If the user
requests the page again, the way the browser displays the same page, is
to resend the same variables.

there are several solutions.  one is this:

1. when form submitted via post, send the email, then
2. imediately redirect to the same page, maybe with a querystring
paramater of emailsent=1 and username=Joe Public.

When the page is displayed with the GET emailsent parameter set to 1,
then you can say "Thanks you Joe Public for sending the email." or

Re: Refresh problem and posts

Quoted text here. Click to load it

What kind of variable?  HTML form?  Javascript?  PHP?

Quoted text here. Click to load it

What kind of code?  PHP?

Quoted text here. Click to load it

Resetting a $_GET or $_POST variable is meaningless after the PHP
code finishes executing.  If the page is submitted again, it
gets its new value from the page.  Ordinary PHP variables vanish
when the code exits.

I'm not so sure Javascript variables retain their values in this
situation either.

$_SESSION would retain its value if you properly start up a session
at the beginning of the PHP code.

Quoted text here. Click to load it

I doubt cookies would help in this situation, except for their
use in establishing a session.

Quoted text here. Click to load it

Database and/or session variables.

                        Gordon L. Burditt

Re: Refresh problem and posts

Gordon is need to keep some sort of state -- either in a
session variable or cookie.

What I usually do (and believe me, it isn't perfect) is to set some
random identifier at the top of the page.  Call it $curPageId.
Whenever I draw a post form, I include the value of curPageId as a
hidden post parameter.  At the top of the next page, set another
session variable called $prevPageId to the value of $curPageId, and set
a new random value for $curPageId.  Then, before processing the form,
check that the value of $prevPageId from the session is the same as the
value of the form variable that was submitted.  If it is, you can
process the form.  If they are different, you know that the form was

Of course, if you want to store the value in a cookie instead of a
session, that's fine, too -- although I tend to prefer the session.

As I said before, the approach isn't perfect.  You can, of course, run
into problems with multiple windows or tabs viewing the same pages.

Re: Refresh problem and posts

Kimmo Laine wrote:
Quoted text here. Click to load it

Unless next_page.php generates PHP, the script with this include will
only get HTML.

Quoted text here. Click to load it


    if (isset($_GET['foo'])) {
      echo '<?php echo $_GET[\'foo\']; ?>';
    } else {
      echo '<?php echo \'Not available\'; ?>';

File not found: (R)esume, (R)etry, (R)erun, (R)eturn, (R)eboot

Re: Refresh problem and posts

Quoted text here. Click to load it

Thanks as ever for a great response and new ideas.

I did as Douglas suggested and reloaded the page with a GET which I
could test for. It also provided a thank you page which can then be
refreshed with no repercussions as Peter advised.

BTW its not about not being bothered but finding elegant solutions.

My thanks to a great group once again.


Site Timeline