3. Dynamically generate form
When generating a form, the program defines records according to each input field in the Definitons table, and generates the corresponding form HTML code and JavaScript code in turn. The first thing to generate in HTML code is text tags:
The following is a quoted snippet:
sHTML = sHTML & vbTab & "< TR >" & vbCrLf & vbTab & vbTab
sHTML = sHTML & "< TD VALIGN=" & Chr(34) & "TOP" & Chr(34)
sHTML = sHTML & " >" & vbCrLf & vbTab & vbTab & vbTab
sHTML = sHTML & "< B >" & ("Label")
The program then checks whether the current input field must be entered. If necessary, add an asterisk after the label text (indicating that the value must be entered), and for the value that must be entered, the corresponding JavaScript code must be generated to verify it. For radio buttons or selection lists, further check that the user has indeed selected an option; for all other input types, just check that the input value is not empty.
Immediately following the text tag are input elements of the form, and the HTML code for these elements is generated based on the types and attributes specified in the Definitions table. Next is to generate JavaScript code to perform client verification tasks based on the input value requirements. For this example, only the numeric value needs to be further checked to ensure that the user's input is indeed a numeric, and that the numeric value is between the maximum and minimum values of the permit. After generating the above code, you can end a table row (that is, an input field) and continue to process the next record of the Definitions table. Once all database records are processed, the next step is to add the HTML code of the "Submit" button and the "Clear" button. If we look at it from another perspective, the program's task here is to generate each input field based on the database records. Each input field occupies a table row, and each table row has two units: the first unit is used to display the text label, and the second unit displays the input element itself (see code).
After the above process is over, the HTML code and verification of the form are saved in the variables sHTML and sJavaScript respectively using JavaScript functions. Before writing these contents to the page, the program checks whether the client requires JavaScript verification. If this type of verification is not required, clear the sJavaScript variable:
If iValType = 0 or iValType = 2 Then sJavaScript = ""
After outputting the BODY tag, the program outputs the following JavaScript function:
The following is a quoted snippet:
< SCRIPT LANGUAGE="JavaScript" >
< !--
function validate(TheForm){
//Client form verification
< %=sJavaScript% >
return true;
}
function CheckRadio(objRadio){
//Is there a certain value in the radio button selected?
for(var n = 0; n < ; n++){
if(objRadio[n].checked){
return true;
}
}
return false;
}
function CheckList(objList){
//Whether a value has been selected in the selection list?
for(var n = 1; n < ; n++){
if([n].selected){
return true;
}
}
return false;
}
//-- >
< /Script >
If the client does not require JavaScript verification, there is only one "return true" statement left in the validate function. The last two static JavaScript functions (CheckRadio and CheckList) in the above code are used to verify radio buttons and drop-down list boxes, and the validate function will call them when these two input fields require verification.
Now you can start writing the form to the page:
< FORM ACTION="./" METHOD="POST" NAME="MyForm" onSubmit="return validate(this)" >
Here, the form submission operation is performed only when the validate function returns true. Therefore, when the client JavaScript verification function is turned off, the validate function will automatically return true.
Next, we want to join a hidden domain called val. As mentioned earlier, this value indicates the verification mode of the form.
< INPUT TYPE="HIDDEN" NAME="val" VALUE="< %=iValType% >" >
When the user submits a form, the processing script determines whether to perform server-side verification based on this value.
Then the table mark and table title are output. The title is saved in the variable sTitleLabel, which is initialized when the script starts executing:
The following is a quoted snippet:
< TABLE BORDER="0" >
< TR >
< TD COLSPAN="2" ALIGN="CENTER" >
< H2 >< %=sTitleLable% >< /H2 >
< /TD >
< /TR >
Previous page123Next pageRead the full text