Run Code Only When Form Is Submitted

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:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes