ASP & POST form control arrays: The more elequent solution
am 15.11.2005 02:36:46 von Scott ButterworthI see a lot of posts on this topic in a lot of different groups that
all seem to give the "standard" approach of appending a unique
identifier to the end of the ID/Name string. Example:-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Then when the form is submitted they use code like this:-
<%
Dim QuestionCount
Dim CurrentAnswer
Dim i
QuestionCount = Request.Form("QuestionCount")
If (Not IsNumeric(QuestionCount)) Then QuestionCount = 0
If (QuestionCount > 0) Then
For i = 1 To QuestionCount
CurrentAnswer = Request.Form("CurrentAnswer" & CStr(i))
~~ Do whatever ~~
Next
End If
%>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This is somewhat messy but its mostly OK and can be used for the
majority of controls apart from checkboxes...
Checkboxes as we all know do not submit any value at all unless the
checkbox is in the checked state at the time the form is submitted and
this can cause code like the example above some problems when
attempting to parse the submitted checkboxes in serverside script.
For example lets say youve just downloaded a list of cars from the
database and you've dynamically built the following client-side code:-
action="favouritecars.asp">
/>
/>
~~ etc, etc. ~~
The user is supposed to check his/her favourite cars and click submit.
Once they do that if you were to issue ASP code to print out all the
Request.Form vars you would probably see something like this:-
chkFavouriteCar1=Honda Accord
chkFavouriteCar45=Buick Regal
chkFavouriteCar57=Rolls Royce Silver Shadow
~~ etc ~~
As you can see there are gaps in the sequence because the user only
checked the checkboxes next to his favourite cars (and the unchecked
boxes were not posted at all).
This of course makes it a tad more annoying to play with on the server
unless you use some creative scripting (eg. assign and check length or
perform string pattern matching on the form var keys)
While I am aware you can also get around this with some simple pre-post
javascript theres a much better way that for some reason most people
(even so-called experts) seem to be unaware of:-
YOU *CAN* SIMPLY AND EASILY POST AN "ARRAY" OF CONTROLS USING A FORM IN
ASP (or any server scripting language)
The key is to use both the "id" and the "name" properties as follows:-
action="favouritecars.asp">
name="chkFavouriteColors" value="Red" />Red
name="chkFavouriteColors" value="Blue" />Blue
name="chkFavouriteColors" value="Green" />Green
name="chkFavouriteColors" value="Purple" />Purple
Note the "[]" at the end of the id attribute (but not in the name
attribute).
Doing this will effectively make the form return
Request.Form("chkFavouriteColors") as an IStringList of values.
This allows you to use neater standard code like:-
Dim PickedColorCount
Dim i
If Not (Request.Form("chkFavouriteColors") = "") Then
PickedColorCount = Request.Form("chkFavouriteColors").Count
For i = 1 To PickedColorCount
Response.Write(Request.Form("chkFavouriteColors")(i) & "
")
Next
Else
Response.Write("No colors were picked")
End If
Note that IStringList is a form of collection and thus its baseindex is
1 (http://webcoder.info/reference/IStringList.html)
This technique can be applied to any input control type, not just
checkboxes.