Microsoft Knowledge Base Article
This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved.
Terms
of Use |
Trademarks
Article ID: 198808 - Last Review: July 16, 2004 - Revision: 3.3
How To Post a Dynamic String Array from the Client to an ASP
This article was previously published under Q198808
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.
- Create a new ASP.
- 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>
APPLIES TO
- Microsoft Active Server Pages 4.0
- Microsoft Visual InterDev 6.0 Standard Edition
- Microsoft Internet Information Server 4.0
- Microsoft Internet Information Services 5.0
| kbcodesnippet kbhowto KB198808 |
Community Feedback System
Very often, it takes hours to solve a problem. Very often, you've looked high
and low, and have tried a lot of solutions. When you finally found it, chances
are, it was because someone else helped you. Here's your chance to give back.
Use our community feedback tool to let others know what worked for you and what
didn't.
Please also understand that the community feedback system is not warranted to be
correct, it's simply a system that we've built to let people try and help each
other. If something in a feedback response doesn't make sense to you, or you're
not comfortable making changes that the feedback talks about (like registry
edits), please consult a professional.
Thank you for using kbAlertz.com Feedback System.
-- Scott Cate