Run Code Only When Form Is Submitted

Run Code Only When Form Is Submitted

I have a Web page that uses a form to submit information to an ASP page. Once the data is submitted, the page is reloaded. At this point, if the client refreshes the screen, the form is resubmitted a second time. Is there any way to clear the form or stop it from resubmitting?


It sounds as if you have some code in the page that you intend to run only if the page is actually submitted using a submit button (or client-side scripting). You need a way to tell your code that there is a section of code that you intend to run only when the page is submitted, but not under any other circumstances. You could simply check the value of one of your form controls using this syntax:

If Len(Request.Form("UserName") 

Alternatively, you could use a hidden field to hold a flag. If the flag is TRUE, your page runs the "special" block of code. If the flag is false, it doesn't. For example, as the page loads create a hidden field and give it a value:

A hidden control is like an invisible textbox. The user can't see it, but it can hold a value. You can use that value as your flag. When the user clicks the submit button, check the value of the hidden control at the beginning of the ASP page that will run:

Dim strUserNamestrUserName  = Request.Form("MYFLAG")If strUserName = "TRUE" Then     ' EXECUTE THE SPECIAL CODEEnd If

Finally, inside of the "SPECIAL" code, you should write out a hidden control in place of the original one. This time it has a different value:

Response.Write ""

The next time that page runs, the "SPECIAL" code will not execute because the flag is set to false.


Share the Post: