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 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:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS