On the Internet, we encounter countless forms every day. We also see that most of these forms do not limit users to submit the same form multiple times. Lack of such restrictions may sometimes produce unexpected results, such as repeated email subscription or repeated voting. Some ASP beginners may not know how to restrict repeated submission of the same form in ASP applications, so here we will introduce a simple method to prevent users from submitting the same form multiple times during the current session.
This work is mainly composed of four subprograms. In simple application scenarios, you only need to put the code in the inclusion file for direct reference. For complex environments, at the end of the article, 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, it will only execute once during each session:
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:
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:
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 checkfid () function is used to complete this task. If it has been registered, false is returned; otherwise, true is returned:
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 above four subprograms have been put into the file forms. Inc. The following Code determines whether to generate a form or process the form result based on the FID value. The processing process described by this subprogram is suitable for most ASP applications:
<% 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:
<%
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. Before processing, you should call checkfid () to check whether the current form has been submitted. The Code class 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 application, it may need to be improved from many 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 to return the result and 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, you need to use cookeis or database to save the relevant data.