A complex form contains multiple input types, such as drop-down list box, single-line text, multi-line text, and numeric values. When you often need to replace such forms, you need a dynamic form generation program. This article describes a system that stores form definition data in a database, dynamically generates form HTML code using ASP scripts, and verifies the form input script.
1. Define the database table structure
Forms such as weekly surveys are often seen on the Web. This is a form that needs to be updated frequently. If there is a program that dynamically generates the form and its verification script, it can greatly reduce the workload of making these forms.
In the dynamic form generation and verification examples in this article, we use an Access database to store the definition information about the form, the data entered by the user in the form is also saved to the same database. Two tables are required to define a form: the first table (Definitons) is used to define the input fields of the form, and the second table (Lists) Stores additional information about each input field, such as selecting items in the list.
Table Definitons contains the following fields:
FieldName -- assign the variable name of the form input field
Label -- text Label, indicating the text displayed before the input field
Type -- a single character that represents the form of the form input field and the Type of the input value
The details are as follows:
(T) text input box, that is, <input type = "TEXT">.
(N) text input box, but enter a number value.
(M) Remarks content, used for comments or input of a large number of texts. It is a multi-line text editing box.
(B) enter "yes" or "no ". In this implementation, check boxes are used to obtain such input. The text label of the check box is "yes ". If you select it, the return value is "on ".
(R) Single-choice button.
(L) drop-down list box.
Min -- only valid for numeric input values. The minimum value is given here. In this example, there is an "Age" (Age) number input box, and its minimum value is set to 1.
Max -- the value of this field is related to the input field format. For the number input box, it indicates the maximum allowed value. For example, the Max value of "Age" is 100. For text input boxes, Max indicates the maximum number of characters allowed. In the multi-line text editing box, Max indicates the number of lines of text in the visible area.
Required -- indicates whether the input is Required. If this type of value is not input, the input validators report an error. In the form, the value that must be entered is marked with a star number, and the user is prompted in the form of a footer that this type of value must be entered.
The example form in this article is an ASP programmer questionnaire. The definition of this form in the Definitons table is as follows:
FieldName Label Type Min Max Required
Name text (t)-50 no
Age (n) 1 100 No
Sex radio button (r)--Yes
E-mail address text (t)---Yes
Language Programming Language drop-down list box (l)--No
The Lists table is used to save some additional information about the input domain definition. In this example, there are two input values: "Sex" and "Ages. The Lists table is very simple and contains only the following three fields:
FieldName -- form input field of the current record
Value -- Value of the selected item
Label -- displays the prompt Text of the selected item.
The input field "Sex" can only be selected from two values: "male" or "female ". "Language" lists several programming languages that can be used in ASP environments, including VBScript, JavaScript, C, Perl, and "others ".
The third table "Records" stores the content submitted by the user. It also contains three fields. Each record corresponds to one submission by the user:
Record -- remarks type, which is saved as a query string.
Created-the date and time when the user submits the form. RemoteIP: the IP address of the form submitter.
In practice, you may need to collect more information about users. In this example, only the additional information of the submission time and the user IP address are recorded.
2. Preparations
After defining the above data structure and form, you can write a script. The script task is to generate a form and process the form submitted by the user.
Whether it is form generation or processing, the following three processes (tasks) are essential: the first is to determine the verification type. When the form is generated, the verification type value is obtained through the query string, read from the hidden fields of the form when processing the form. The program supports the following four types of Form Verification Methods: no verification, client JavaScript verification, and Server ASP script verification, verify both the client and the server (Code: 0 to 3 ). If a valid authentication method is not specified in the query string, the fourth authentication method is used by default. This verification processing method allows us to flexibly apply this form generation and processing system. When the client prohibits JavaScript verification, we can only execute the verification process on the server. The following code determines the verification type:
Check verification type
The following is a reference clip:
IValType = Request. QueryString ("val ")
If IsNumeric (iValType) = False Then iValType = 3
If iValType> 3 or iValType <0 Then iValType = 3
The second task is to open the database connection and create two record set objects: RS object, which is the main record set object in this program, used to operate the Definitions table; RSList object, it is mainly used to read data from the Lists table. The sample program provides two Database Connection Methods: using odbc dsn or not using odbc dsn (when using DSN, you must first create a DSN named Dynamic, the code for connecting to the database using DSN has been commented out ).
The third task is to output some static HTML code before (and after) The form script is generated (or processed), such as <HEAD> </HEAD>, the resources occupied by RS, RSList, and other objects are released at the end of the script operation.
In addition to the code used to complete the preceding tasks, other ASP scripts in the example application may generate two types of pages: question form (see) and the results page after the form is submitted (the latter is also responsible for records of the results submitted by the user ). To determine which script to run, the simplest way is to check whether a form has been submitted: If yes, process the form; otherwise, generate the form.
Are forms generated or processed?
If Len (Request. Form) = 0 Then
'Generate a form
...
Else
'Process the form
...
End If