How to stop a page from refreshing

I'm using vbscript and ASP to write info to a database and to create a file. This all works fine, but if I refresh the page all sorts of horrible things happen. I get errors about not being able to insert duplicate primary keys etc....

So my question is how can I prevent users from refreshing my page?

I've tried to send a variable with the url using get, in the hopes that this would allow me to determine if the page had been linked from the correct page. If not then I would know not to run the code that if run more then once would create an error but it seems that if I refresh the page AFTER it has come from the correct page the get info is also refreshed which means that that idea won't work.

Any ideas anyone?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

well, i don't know your code, but i think keeping people from refreshing is a very cosmetic solution. Better rewrite your code.

Keeping people from refreshing is not really possible, it is something which is part of the browser. there are trick possible, but it's not stopping 100%.

I like to code:

Edit.asp (form, posted to Action.asp)
Action.asp (processes the data, and redirects to another page)

So after the form has been submitted the visitor comes at a completely different page that does nothing with the database. refreshing is no problem then
IarfhlaithAuthor Commented:
thanks. I'll have another look at it. And cheers for the quick response.

That doesn't really eliminate the problem either.  Its easy for a user to the back button to go back to the previous page and submit whatever again.  I know you're probably thinking they wont do that...I thought the same thing at one time.  But basically anything they can do, they will do.

So your best bet is to implement some error handling.  You can either check first to see that the data you are inserting is unique, or you could use "On Error Resume Next" and check Err.Number after you execute the SQL statement to see if it succeeded or not.
I don't know: how can you get duplicate primary keys after a submit?

idea: you have an Autonumber primary key, then there's never the problem of a duplicate primary key
idea: your primary is not autonumber, but still numeric, you'll generate a unique number on every insert - no duplicate primary keys
idea: your primary key is non-numeric (i'd really recommend using a numeric primary key, if other fields must be unique, then create a unique index or so) - in this case you'll have to check if the submitted valu is unique before inserting and return a warning if it is not unique- no duplicate primary keys

i don't know how your code can generate duplicate primary keys.

MannSoft is right: if users *can* use the backbutton and resubmit the same data again, they *will* do it. It happens here on EE also. But the problem has been reduced enormously since the code has been changed, and "refresh" after a "submit" now no longer re-inserts the last comment. That's because of the Edit-page submitting to some "action", which redirects to the Edit-page again. Now if you hit refresh after submitting a comment, you just reload the edit-page.

Of course you could check if exactly the same data is already present in the database, and if so, just don't insert it.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
IarfhlaithAuthor Commented:
Thanks guys.

I've solved the problem by splitting up the code onto 2 pages. After the form is submitted, One page just for the script to insert to the database and then a quick re-direction to another that only holds the HTML for the user. That way, if they press the refresh button (and apparantly they will!) they only refresh the HTML and not the script. The use of the back button will also bring them back to the form and completely skip over the script only page.

The reason I was getting duplicate primary keys was I'm letting the user choose a name as one of the fields in the form. This name IS the primary key. Under normal use I do a simple check to see if that name exists in the database already and i protect against it that way. But this lttle select and compare is done on the form page as part of the form validation and isn't run when the user refreshes the next page.

So I've solved the problem by splitting the page into two: one for the script and one for the HTML.


It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.