HOWTO: Creating a Dynamically Growing Form Using ASP

Last reviewed: December 11, 1997
Article ID: Q163499
1.00 WINDOWS NT kbprg kbcode kbhowto

The information in this article applies to:

  • Microsoft Active Server Pages, version 1.0

SUMMARY

This article provides an example of how to simulate a dynamically growing form using Active Server Pages. This example allows the user to enter items one at a time, and add them to a list. Once the list has been created, it can be submitted as a whole.

MORE INFORMATION

In the following example, a basic form is drawn with one text box. The user can enter information in that text box, and press the 'Add Item to List' button. This button submits the form to the same file. As each item is entered, it is added to the form as a new text box with the name ITEMS. Ultimately when the whole list is submitted, there are several items in the list that can be looped through as a collection of ITEMS.

To test this technique, save the text below to a file named Dynaform.asp. Be sure that this file is located in an IIS Virtual Root.

File: DYNAFORM.ASP

   <%@ language = vbscript%>
   <% Response.Expires = 0 %>
   <HTML>
   <HEAD>
   <TITLE>Dynamically Growing Form</TITLE>
   </HEAD>
   <BODY>
   <%
   If Request("Action") = "Submit the List" Then
     ' Show what was entered.
     Response.Write "<B>Here are the Items submitted:</B><BR>"
       nItems = Request.Form("Items").Count
       For i = 1 To nItems
         ' Show submitted Items
         Response.Write Request.Form("Items")(i) & "<BR>"
         Next
         Response.Write Request("Item") & "<BR>"
   Else
     ' Create the form from all items. %>
     <FORM Action=dynaform.asp Method=Post>
     <B>Items:</B><BR>
     <%
     nItems = Request.Form("Items").Count
     For i = 1 To nItems
       ' Show previously submitted Items
       Response.Write "<INPUT Type=Text Name=Items Value=""" & _
         Trim(Request.Form("Items")(i)) & """><BR>"
     Next

     If Request.Form("Item") <> "" Then
       ' paint a new input box, and store the old Item in Items collection
       Response.Write "<INPUT Type=Text Name=Items Value=""" & _
         Trim(Request.Form("Item")) & """><BR>"

       Response.Write "<P>Please enter an Item,<BR>"
       Response.Write "and submit them one at a time<BR>"
       Response.Write "by pressing the Add Item button.<BR>"
       Response.Write "<INPUT Type=Text Size=50 Name=Item Value="""""">"
     Else
       ' No Item was submitted, don't show an error
       Response.Write "<P>Please enter an Item,<BR>"
       Response.Write "and submit them one at a time<BR>"
       Response.Write "by pressing the Add Item button.<BR>"
       Response.Write "<INPUT Type=Text Size=40 Name=Item Value="""""">
       <BR>"
     End If
   %>
     <P>
     <INPUT Type="Submit" Name="Action" Value="Add Item to List">
     <INPUT Type="Submit" Name="Action" Value="Submit the List">
     <BR>
   <% End If %>

   </FORM>
   </BODY>
   </HTML>

REFERENCES

For the latest Knowledge Base articles and other support information on Visual InterDev and Active Server Pages, see the following page on the Microsoft Technical Support site:

   http://support.microsoft.com/support/vinterdev/


KBCategory: kbprg kbcode kbhowto
KBSubcategory: AXSFHTML
Additional reference words: 1.00
Keywords : AXSFHTML kbcode kbhowto kbprg
Version : 1.00
Platform : NT WINDOWS


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: December 11, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.