Hello everyone, today I want to talk about how to use ASP to dynamically add Form items in Html documents. If you are familiar with Html, you must know that there is an <form> </form> HTML Tag. There are tags (elements) between <form> and </form>, such as Text, Password, Button, Submit, and Reset ). The dynamic add Form item here refers to the dynamic add Text element in <Form> </Form>. How can we dynamically add Form items? First, we will analyze and analyze how to dynamically add form items. If you want to add one or more form elements of the text type by writing Html documents at the same time by yourself or using such Html documents as FrontPage and DreamWeaver. You must write the form element of the first type of text, and then add the form element of the second type of text until the number of conditions is met. When adding a new text-type element, we should also display other form elements that already exist and have content and type as text. The above mentioned method of adding form elements of the text type by compiling Html documents statically is actually the basic idea of dynamically adding form elements of the text type in ASP program files. How can we get the content of other form elements that already exist and have content and type as text? You can use request. form ("TextFieldName") in ASP. "TextFieldName" indicates the entry named "TextFieldName" in <form> </form>. It refers to "TextFieldName" in <form> <input type = text name = "TextFieldName"> </form> ". Now that we know how to get the content of the form, we can use this method to get the value of the form element that already exists and has content and type as text. Then, when we display the text items with values, we can assign the obtained values to the corresponding items. Next, we will want to know how many form items with content and type as text? We can use this method. This is obtained through the Count attribute of request. form ("TextFieldName. This is because when we obtain multiple form elements of the same name as Text, we can get a set of values, that is, their values are placed in a set named TextFieldName. At this time, the count attribute counts the number of similar elements in this set, so that we can know the total number of form elements of the same type as text, you can use the cyclic statement and the count attribute to retrieve the values of the set. You may ask: how can we get the value of each form Element in the Set named TextFieldName text? Before that, we first define a variable I, whose initial value is 1. In fact, this variable I is the variable we are doing in the loop. Next, we can use the request. form ("TextFieldName") (I) method to obtain the value of each form element named TextFieldName whose type is text. Note that when we extract and assign the value of the form element with a value and a type of text to the corresponding item and display it, we should also add a new form item of the text type. Why? This is to allow us to continue adding new values. Otherwise, after you retrieve all form elements with a value of the text type, you will not add new values, the Form item in the Html document cannot be added dynamically. Therefore, you must not forget this important point. Now we know how to obtain the number of form elements that meet the condition type of text, and how to obtain their respective values. Then, how can I control only the elements of the form with values and types of text? And how can we implement everything we mentioned above? Then I will give its source code and add comments or explanations in some places.
'/* DynamicAddForm. asp File source code start point */<% @ Language = VBScript %> <% 'authorization' Author: WaiWai (Skew) 'created Date: 2000-2-20 'File Name: DynamicAddForm. ASP 'description: Dynamic Add Form's Text Fields. 'All Rights Reserved. ownership belongs to City Club. '-------------------------------------------- %> <HTML> <HEAD> <meta name = "GENERATOR" Content = "Microsoft Visual Studio 6.0"> <title> Dynamic Ad D Form Text Elements. </title> <style type = "text/css"> <! -- Td {font-size: 9pt} body {font-size: 9pt} select {font-size: 9pt} A {text-decoration: none; color :# 003366; font-size: 9pt} A: hover {text-decoration: underline; color: # FF0000; font-size: 9pt} --> </style> </HEAD> <BODY> <table border = "1" cellspacing = "0" height = 400 width = 98% bordercolorlight = "#5E5E00" bordercolordark = "# FFFFEC" bgcolor = LightGrey align = center valign = top>
<% If trim (Request. form ("List") = "show existing items" then'/* Where the trim () function removes the Request. space on both sides of Form ("List */
'/* When you click the button "show existing items", we will see all form items with the value type of text */Response. write "<td> <form> <center> the stored element content is:" & "</td> <tr>" for I = 1 to Request. form ("items "). count '/* Number of existing Text types */Response. write "<td align = center>" & Request. form ("items") (I) & "</td> <tr>" nextif trim (Request. form ("newItem") <> "thenResponse. write "<td align = center>" & trim (Request. form ("newItem") Response. write "<input type = button name = 'bac K'value = 'back' Response. write "style = 'font-size: 12pt 'onclick = 'window. history. back () '>'/* click the <Back> button to return to the previous page, that is, the page where the Form item with the type of text is added */Response. write "</form> </td> <tr>" end ifelse '/* indicates that the user has clicked the "add" button to add the user */Response. write "<td align = center> <form action = DynamicAddForm. asp method = post> </td> "for I = 1 to Request. form ("items "). count '/* In this case, the form Element */'/* Request that already exists and has a value type of text is displayed cyclically. form ("items "). cou Nt is the number of existing Text types */'/* We can add conditions to determine the added content here. Such as determining whether the length of the added content is less than 3. */Response. write "<td align = center> <input type = text name = items value = '" Response. write Request. form ("items") (I) & "'> </td> <tr>"'/* this step shows the value of the form element with a value of the text type. */nextif trim (Request. form ("newItem") <> "then"/* this step is used to check whether the user has added a new value to the form element with the new type of text, */'/* If yes, this value is assigned to the form element whose name = items type is text and displayed. */Response. write "<td align = center> <input type = text name = items value = '" Response. write trim (Request. form ("newItem") & "'" Response. write "> <br> </td> <tr>" Response. write "<td align = center> <input type = text name = newItem" Response. write "value =''> </td> <tr> "'/* Add a form element named newItem text to allow you to continue adding the element. */Else '/* indicates that no new value is added to the form element of the new text type, therefore, you can only list */'/* a form element named newItem as text, allowing you to continue adding the element. */Response. write "<td align = center> <input type = text name = newItem value =''> </td> "end if %> <tr> <td align = center> <br> <input type = submit name = "List" value = "show existing items" style = "font-size: 12pt "> <input type = submit name =" Add "value =" Add "style =" font-size: 12pt "> </form> </td> <% end if %> </table> </BODY> </HTML> '/* DynamicAddForm. source code end point of an asp file */
|
After reading the source code, you may notice that there are two form item elements of the text type: one is named items and the other is named newItem. Why? This is because we need to distinguish which ones are obtained by clicking the <add> button and entering a new value, which form item elements can be input with the new value type of text to enable users. In this way, we can clearly list only the form item elements that the user has added with a value type of text in the loop. That's why we name them into different names. Isn't that wonderful? Haha :-). In fact, the key to this method is to use the request. the Count attribute and request of form ("TextFieldName. form ("TextFieldName") (I) method makes it easier for us to obtain the number of form item elements whose type is TextFieldName and their values respectively. These two methods are very useful and can greatly simplify the code we want to write. You must remember these two usage methods. Next, I will give you an ASP program source code that does not use the two methods. You can compare them and see why the above Code is better.
| '/* Start point of the source code of the DynamicAddForm2.asp File */<% @ Language = VBScript %> <% 'authorization' Author: WaiWai () 'created Date: 2000/1/13' File Name: dynamicAddForm. ASP 'description: Dynamic Add Form's Text Fields. 'All Rights Reserved. ownership belongs to City Club '-------------------------------------------------- %> <HTML> <HEAD> <meta name = "GENERATOR" Content = "Microsoft Visual Studio 6.0"> </HEA D> <style type = "text/css"> <! -- Td {font-size: 9pt} body {font-size: 9pt} select {font-size: 9pt} A {text-decoration: none; color :# 003366; font-size: 9pt} A: hover {text-decoration: underline; color: # FF0000; font-size: 9pt} --> </style> <title> Add new mail server site. all Rights Reserved. </title> <script language = javascript> function mycheck (tt) {alert ("afd ") return false} </script> <BODY topmargin = 12> <form name = form1 method = post> <tab Le border = "1" cellspacing = "0" height = 400 width = 98% bordercolorlight = "#5E5E00" bordercolordark = "# FFFFEC" bgcolor = LightGrey align = center valign = top> <td align = center colspan = 5 valign = top height = 30> |
Now we can see the source code of each of these two methods. What do you think? Oh, do you think the first program code is much simpler than the second program code. In the first program code, we used request to obtain the value of form element with the same name type as text. the Count attribute of form ("TextFieldName") to Count the total number of such form elements so far. In the second program code, we store and obtain this number through the element of the type "hidden" in <form> </form>. Another point worth mentioning is to name all the form elements of the text type with the same name. This avoids the section of the second program code, such as "url =" url "& I. Of course, this practice will come to mind only when you know that request. form ("TextFieldName") has the Count attribute. Therefore, the advantage of the first program code is that it makes full use of the Count attribute and value method when obtaining the value of the form element of the same name. The good ideas and methods for solving the problem are based on a thorough understanding of the tools used .. The key to all this is that we must continue to do so that we can better understand and understand, learn, and have better and better methods.