Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: E-Commerce
Expertise: Beginner
Aug 24, 1999

Run Code Only When Form Is Submitted

Question:
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?

Answer:

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") < 1) Then
    ' THE USER ENTERED SOMETHING...THIS IS
    ' PROBABLY A SUBMIT.
End If

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 strUserName
strUserName  = Request.Form("MYFLAG")

If strUserName = "TRUE" Then
     ' EXECUTE THE SPECIAL CODE
End 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.

DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date