ASP & POST form control arrays: The more elequent solution

ASP & POST form control arrays: The more elequent solution

am 15.11.2005 02:36:46 von Scott Butterworth

I 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:-

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


<%
Dim i
i = 0

rsQuestions.MoveFirst
If (Not rsQuestions.eof) Then
i = 1
Do While (Not rsQuestions.eof)
%>

Q<% =i %>
<% =CStr("" & rsQuestions("QuestionText")) %>
">

<%
rsQuestions.MoveNext
If (Not rsQuestions.eof) Then i = i + 1
Loop
End If
%>




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">
valign="top">












~~ etc, etc. ~~




/>
Honda Accord

/>
Honda Civic


Mini Metro






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.

Re: ASP & POST form control arrays: The more elequent solution

am 15.11.2005 03:01:53 von reb01501

Ummmm, hate to burst your bubble, but:

Remove the brackets from the id's and try your code again ...

The brackets have nothing to do with the result, which is entirely due to to
the fact that you have multiple elements with the same name attribute. In
fact, remove the id attributes entirely and try it.

In fact, try this:

for each key in Request.Form("chkFavouriteColors")
Response.Write key & "
"
next


Bob Barrows

Scott Butterworth wrote:
> I 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:-
>

--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"

Re: ASP & POST form control arrays: The more elequent solution

am 15.11.2005 17:30:22 von Scott Butterworth

My bubble hath been burst!

The reason I included an id field with the [] was because i first
started using this technique on an Apache PHP platform and that was the
way I was *taught* to do it in order to have PHP recognise the posted
var as an array of values.

Bob is correct, in ASP you can simply use the same name attribute for
all of your controls and it will form an IStringList automatically when
the form is posted which beats have to worry about giving all of your
checkboxes a unique id.

Re: ASP & POST form control arrays: The more elequent solution

am 15.11.2005 17:42:43 von reb01501

Scott Butterworth wrote:
> My bubble hath been burst!
>
> The reason I included an id field with the [] was because i first
> started using this technique on an Apache PHP platform and that was
> the way I was *taught* to do it in order to have PHP recognise the
> posted var as an array of values.
>
> Bob is correct, in ASP you can simply use the same name attribute for
> all of your controls and it will form an IStringList automatically
> when the form is posted which beats have to worry about giving all of
> your checkboxes a unique id.

The unique id is useful in clientside script. For example, give a set of
checkboxes the same name and there is no way to deal with them individually.
Give each a unique id, and now you have a handle to the individual elements.

Bob Barrows
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.