Restricting duplicate submission of the same form _ Application techniques in an ASP application

Source: Internet
Author: User
We encounter countless forms on the Internet every day, and we see that most of them do not restrict users from submitting the same table multiple times
Single. The lack of such restrictions can sometimes produce unexpected results, such as repeating a subscription to a mail service or repeating a ballot. Maybe some
ASP beginners do not know how to limit the repeated submission of the same form in ASP application, so here to introduce to you in the ASP application to prevent
A simple way for users to submit the same form multiple times during the current session.

This work is mainly composed of four subroutines, in a simpler application, you simply put the code in the containing file straight
To the more complex environment, we give some suggestions for improvement at the end of the article.

First, the basic work process

Let's discuss the four subroutines in turn.

(i) initialization

Here we want to save two variables in the Session object, where:

⑴ each form corresponds to a unique identifier called a FID, in order to make the value unique to use a single counter.

⑵ each time a form is successfully committed, it must store its FID in a Dictionary object.

We use a dedicated process to initialize the above data. Although each subroutine will call it later, in fact every
It is executed only once during the session:

Copy Code code as follows:

Sub Initializefid ()
If not IsObject (session ("Fidlist")) Then
Set session ("Fidlist") =server.createobject ("Scripting.Dictionary")
Session ("FID") =0
End If
End Sub


(ii) Generate unique identifiers for the form

The following function, Generatefid (), is used to generate a unique flag for a form. The function first adds the FID value to 1, and then returns it:
Copy Code code as follows:

Function Generatefid ()
Initializefid
Session ("FID") = Session ("FID") + 1
Generatefid = Session ("FID")
End Function


(iii) Registration of submitted forms

When the form is successfully submitted, it registers its unique identity in the Dictionary object:
Copy Code code as follows:

Sub Registerfid ()
Dim Strfid
Initializefid
Strfid = Request ("FID")
Session ("Fidlist"). Add Strfid, now ()
End Sub

(iv) Check if the form is submitted repeatedly

Before you formally process a user-submitted form, you should check to see if its FID is registered in the Dictionary object. Below the
The Checkfid () function is used to do this work, and returns False if it has been registered, otherwise it returns true:

Copy Code code as follows:

Function Checkfid ()
Dim Strfid
Initializefid
Strfid = Request ("FID")
Checkfid = Not session ("Fidlist"). Exists (Strfid)
End Function

Second, how to use

There are two places to use the above function, that is, when the form is generated and when the results are processed. Suppose the above four subroutines have been put into the inclusion text
In the Forms.inc, the following code determines whether the form is generated or the form results are processed based on the FID value, and the process described is suitable for
For most ASP applications:
Copy Code code as follows:

<%option explicit%>
<!--#include file= "Forms.inc"-->
< html>
< head>
< title> form submission Test </title>
< body>
<%
If Request ("FID") = "" Then
Generateform
Else
ProcessForm
End If
%>
</body>

Generateform is responsible for generating the form, and the form should contain a hidden fid, such as:

Copy Code code 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 content submitted through the form, but before processing you should call Checkfid () to check that the current form is
After submitting, code classes such as:

Copy Code code as follows:


<%
Sub ProcessForm ()
If Checkfid () Then
Response.Write "The content you entered is" & Request.QueryString ("param1")
Registerfid
Else
Response.Write "This form can only be submitted once!"
End If
End Sub
%>

III. Limitations and improvement measures

Above we describe a method that restricts the submission of the same form multiple times during the current session. In practical applications, you may need to
Be improved in many ways, for example:

⑴ Check the legality of user input data before registration form ID, make the data illegal, users can press the "Back" button
Returns and submits the same form again after the correction.

⑵ This restriction on form submission can only be valid during the current session. If this restriction is required to span multiple sessions
, you need to use a Cookeis or database to save the data.
Related Article

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.