Solved

Prevent resend of form info on refresh

Posted on 2003-11-15
9
1,136 Views
Last Modified: 2007-12-19
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
0
Comment
Question by:HeadAcheMike
9 Comments
 

Expert Comment

by:ChikenDude911
ID: 9757524
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...?

0
 
LVL 2

Author Comment

by:HeadAcheMike
ID: 9758138
yes correct
0
 
LVL 2

Author Comment

by:HeadAcheMike
ID: 9768366
...anyone ?
0
 
LVL 2

Expert Comment

by:exile_4
ID: 9770279
a snippet of your code might help here.


Cheers,
Exile_4
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Author Comment

by:HeadAcheMike
ID: 9770498
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?
0
 
LVL 1

Expert Comment

by:Witchazel
ID: 9774660
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.
0
 
LVL 2

Author Comment

by:HeadAcheMike
ID: 9774689
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
0
 
LVL 1

Accepted Solution

by:
Witchazel earned 30 total points
ID: 9776881
oh, one page

(psudoey)

if(we are posting)
     if(key doesnt exists in db)
          save row  
else
     get next key from db
end if

<form>
   <hidden name=key value=key>
</form>

so if you are not posting, you get the nextval from your db, if you are you save but never change the val

 but....

if you are unfortune enough not to have a oracle db (or sequencing) this might not be best for you because you have a high chance of dupping (unless your site is as active as mine :)

so you would have to be a bit tricky cas you dont want to display a bunch of blanks when you pull it out
first you would have to add nother column to your table for active/inactive

(psudoey)

if(we are posting)
     update tablename set active ='yes' .... where key=key
else
     insert into tablename (id,active) values (mynextid,'no')
end if

<form>
   <hidden name=key value=mynextid>
</form>


note when you pull from this table you will hafta do a where active ='yep'

-w
0
 
LVL 2

Expert Comment

by:smu95rp
ID: 10211137
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
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Someone recently asked me about how to display a progress indicator on a page while an iframe is loading. And I remember when I first came across this myself. It was a bit tricky to get my head around, but really, it's very simple. The most impor…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
In this tutorial viewers will learn how add a full-size background image to a webpage using CSS3. Create a new HTML document with an internal stylesheet.: In CSS, define the html element to have a background image. Use a high resolution image.: In t…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now