Restrict repeated submission of the same form in ASP applications

Source: Internet
Author: User

On the Internet, we encounter countless forms every day. We also see that most of the forms do not limit users to submit the same table multiple times.
Single. Lack of such restrictions may sometimes produce unexpected results, such as repeated email subscription or repeated voting. Maybe some
ASP beginners do not know how to restrict repeated submission of the same form in ASP applications, so here we will introduce how to prevent
A Simple Method for users to submit the same form multiple times during the current session.

This job mainly consists of four sub-accountsProgramComponents, in a relatively simple application, you just needCodePut in the contained file directly
For more complex environmentsArticleFinally, we provide some suggestions for improvement.

I. Basic Work Process

Next we will discuss these four subprograms in sequence.

(1) initialization

Here we need to save two variables in the session object, where:

(1) Each form corresponds to a unique identifier called FID. A counter is used to make the value unique.

(2) Each time a form is successfully submitted, its FID must be stored in a dictionary object.

We use a dedicated process to initialize the above data. Although each subroutine will call it in the future
During this period, it only runs once:

Copy code The Code is as follows: Sub initializefid ()
If not isobject (Session ("fidlist") then
Set SESSION ("fidlist") = server. Createobject ("scripting. Dictionary ")
Session ("FID") = 0
End if
End sub

(2) Unique Identifier of the generated form

The following function generatefid () is used to generate the unique identifier of a form. This function first adds the FID value to 1 and then returns it:Copy codeThe Code is as follows: function generatefid ()
Initializefid
Session ("FID") = SESSION ("FID") + 1
Generatefid = SESSION ("FID ")
End Function

(3) registration submitted forms

When the form is successfully submitted, register its unique identifier in the dictionary object:Copy codeThe Code is as follows: Sub registerfid ()
Dim strfid
Initializefid
Strfid = request ("FID ")
Session ("fidlist"). Add strfid, now ()
End sub

(4) check whether the form is submitted repeatedly

Before formally processing a form submitted by a user, check whether its FID has been registered in the dictionary object. The following
The checkfid () function is used to complete this task. If it has been registered, false is returned; otherwise, true is returned:

Copy code The Code is as follows: function checkfid ()
Dim strfid
Initializefid
Strfid = request ("FID ")
Checkfid = Not SESSION ("fidlist"). exists (strfid)
End Function

Ii. How to Use

The preceding functions are used in two aspects: form generation and result processing. Assume that the preceding four subprograms are included in
In forms. Inc, the following code determines whether to generate a form or process the form result based on the FID value.
For most ASP applications:Copy codeThe Code is as follows: <% option explicit %>
<! -- # Include file = "forms. Inc" -->
<HTML>
<Head>
<Title> test form submission </title>
</Head
<Body>
<%
If request ("FID") = "" then
Generateform
Else
Processform
End if
%>
</Body>
</Html>

Generateform is used to generate a form. The form should contain a hidden FID, for example:

Copy code The Code is as follows: <%
Sub generateform ()
%>
<Form action = "<% = request. servervariables (" path_info ") %>" method = get>
<Input type = hidden name = FID value = "<% = generatefid () %>">
<Input type = text name = "param1" value = "">
<Input type = submit value = "OK">
</Form>
<%
End sub
%>

Processform is responsible for processing the content submitted through the form, but before processing, you should call checkfid () to check whether the current form has been
After submission, the Code class is as follows:

Copy code The Code is as follows:

<%
Sub processform ()
If checkfid () then
Response. Write "your input is" & request. querystring ("param1 ")
Registerfid
Else
Response. Write "this form can only be submitted once! "
End if
End sub
%>

Iii. Restrictions and Improvement Measures

The preceding describes how to restrict multiple submissions of the same form during the current session. In practical applications, you may need
Improvements in multiple aspects, such:

(1) check the validity of the user input data before the Registration Form ID. If the data is invalid, you can press the "back" button.
Return. Submit the same form again after correction.

(2) This restriction on Form submission can only be valid during the current session. If this restriction is required to span multiple sessions
So you need to use cookeis or database to save the relevant data.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.