How To Post a Dynamic String Array from the Client to an ASP (198808)



The information in this article applies to:

  • Microsoft Active Server Pages
  • Microsoft Visual InterDev 6.0
  • Microsoft Internet Information Server 4.0
  • Microsoft Internet Information Server 5.0

This article was previously published under Q198808

SUMMARY

This article describes how to submit a dynamic string array from the client to an ASP page. Normally, if you have a fixed-length string array for submitting this to the server, you can create a form with some hidden fields in it, whose value is set to different strings in the array. The number of hidden fields defined in the form will be equal to the number of elements in the array.

However, if the number of elements in the array is not known beforehand (that is, if you are using a dynamic array), you won't be able to use a pre-defined form because the number of hidden fields to be used is unknown while coding the HTML page.

This example shows how to create a form dynamically depending on the number of elements in the array and then submit the data to the server.

MORE INFORMATION

  1. Create a new ASP.
  2. Copy and paste this code into it. Save the file as "try.asp" and preview it in a browser:
    <%@  Language=VBScript %>
    <HTML>
    <HEAD>
    <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
    </HEAD>
    <BODY>
    <B>
    <%
    If Request.Form <>"" Then
      For each intNumItems in Request.Form
        Response.Write Request.Form(intNumItems) & "<BR>"
      Next
      Response.Write "<HR>"
    End if%>
    </B>
    
    <FORM NAME="MyForm">
    <B>Enter the number of elements in the array: </B>
    <INPUT TYPE="TEXT" NAME="numelements" VALUE="5">
    </FORM>
    <BR>
    <INPUT TYPE="BUTTON" NAME="cmdButton" VALUE="Send To Server">
    
    <Script Language="VBSCRIPT">
    Dim MyArray()
    ReDim MyArray(5)
    
    Sub cmdButton_onClick()
      intNumItems=Cint(Document.MyForm.numelements.value)
      If intNumItems <> 0 Then
        ReDim MyArray(intNumItems)
      End if
    
      'Fill in the array with the string of the word VALUE plus the counter number
      For i = 0 to intNumItems
        MyArray(i) = "Value " & i
      Next
    
      Dim i
      i = Ubound(MyArray)
      a = "<FORM NAME=" & chr(34) & "TempForm" & chr(34) & " ACTION=try.asp METHOD=POST><BR>"
      For intNumItems = 1 to i
        a = a & "<INPUT TYPE=" & chr(34) & "HIDDEN" & chr(34) & "Name=" & chr(34) & _
          "item" & intNumItems & chr(34) & "Value =" & chr(34) & MyArray(intNumItems) & chr(34) & "> <BR>"
      Next
      a = a & "</FORM>"
      Document.Write a
      TempForm.Submit
    End Sub
    
    </Script>
    </BODY>
    </HTML>
    					

Modification Type:MinorLast Reviewed:7/16/2004
Keywords:kbCodeSnippet kbhowto KB198808