Link to home
Start Free TrialLog in
Avatar of HeadAcheMike
HeadAcheMike

asked on

Prevent resend of form info on refresh

I have a simple message system set up where as using a form information is inserted into a mysql database

The problem is if you refresh the page after having made a post it prompts the user to retry sending the information, is there anyway to prevent this? i was thinking along the lines of no cache but i wasnt sure if that would work with forms

thanks
Avatar of ChikenDude911
ChikenDude911

ok so you have a message system set up right....but if you refresh the page after you have sent the post it tries to do it again, but you dont want it to do it again if the page is refreshed right...?

Avatar of HeadAcheMike

ASKER

yes correct
...anyone ?
a snippet of your code might help here.


Cheers,
Exile_4
im not sure it will because its just a general question related to all forms, i dont really want to post my code so il just tell you the methods im using

the forms action posts back to the same file (using PHP_SELF), i then get the data using $_POST and run an SQL query with the two posted fields (name and message) whereas they are inserted into the database.

the problem is if the user clicks refresh after having posted a message and then clicks retry on the dialog that appears then then data is resent to the page and therefore the query is run again causing a double entry in the database.

i therefore need to stop the data from being resent, is this possible?
you can pull the table key out when you create the page then when you save to database save it under that key.  if that is taken then it has already been saved.

notice you would need a form page and a post page with this though.
id really like to keep it all in the one file

i figured before using two files would solve this problem anyway because i would have to form action pointing to another file with the query and then use the meta refresh tag to redirect back to the index file. As i said though id like to keep it to one file so this wasnt an option
ASKER CERTIFIED SOLUTION
Avatar of Witchazel
Witchazel

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of smu95rp
I'm trying to find the same thing.

Anyone know any JavaScript that can empty the browser's cache of POSTed data so that subsequent refreshes fail to pass the data again?

It is, I suppose, possible to check for existence of a record before INSERTing it again, however I'd prefer to get JavaScript to redirect to some sort of display record page on refresh.

In the meantime I shall consider redirecting the browser to said page after the data has been posted to the database. I can use something like
"<SCRIPT>window.location.href='show-quote.php?quote_num=<?=$quote_num?>';</SCRIPT>"
in my page somewhere after the database has been updated so that the page in the browser isn't actually the one that the data was POSTed to.

I'll let you know how I get on with that, however if anyone has a better solution...

Rich