processing mutiple checkbox selections in vbscript

N

.Net Sports

I have a form that on the front end Is dynamically generated by
drawing records from a database, then each record is displayed with a
checkbox next to it, where you can click multiple checkboxes, hit the
submit button in the form, and goes to a next page where it will
display the ID # of the record(s) you selected from the form page:
'''''''''''''
<form name="form1" action="postpage.asp" method="post"><input
type="checkbox" id="ID" name="ID" value="<%=RS("meID")%>">
''then the associated records are displayed inline, followed by submit
button
<input type="submit" name="go" id="go" value="GO" class="inputBox"/>

'''''''''''

works fine when selecting one record from the display; below is the
block of code that i am using to output all the ID#s from the form
data i selected:
<%
dim qid
qid = Request.Form("ID")

For each CheckedItem in Request.Form("ID")

response.write qid
next
%>
''''''''''''''''''''''''''''''''
if I select more than one record and hit submit, i will get the ID#s
of each, but i will also receive an extra figure displayed, which is
all the ID#s selected appended to each other. Ex:
1,
2
12
...and the first number will hav a comma appended to the output.
Womdering how to manage the "for each " loop in the code, or how am I
handling the event incorrectly?
 
A

Adrienne Boswell

I have a form that on the front end Is dynamically generated by
drawing records from a database, then each record is displayed with a
checkbox next to it, where you can click multiple checkboxes, hit the
submit button in the form, and goes to a next page where it will
display the ID # of the record(s) you selected from the form page:
'''''''''''''
<form name="form1" action="postpage.asp" method="post"><input
type="checkbox" id="ID" name="ID" value="<%=RS("meID")%>">
''then the associated records are displayed inline, followed by submit
button
<input type="submit" name="go" id="go" value="GO" class="inputBox"/>

'''''''''''

works fine when selecting one record from the display; below is the
block of code that i am using to output all the ID#s from the form
data i selected:
<%
dim qid
qid = Request.Form("ID")

For each CheckedItem in Request.Form("ID")

response.write qid
next
%>
''''''''''''''''''''''''''''''''
if I select more than one record and hit submit, i will get the ID#s
of each, but i will also receive an extra figure displayed, which is
all the ID#s selected appended to each other. Ex:
1,
2
12
..and the first number will hav a comma appended to the output.
Womdering how to manage the "for each " loop in the code, or how am I
handling the event incorrectly?

That is the nature of the beast. The input type checkbox, because it
accepts multiple values, sends name/array pair instead of the usual
name/value pair. So what you want to do is treat what is coming back
from the checkbox request.form("id") as an array, eg:

dim idarr, i
idarr = split(request.form("id"),",")

for i = 0 to ubound(idarr)
response.write i & ": " & idarr & "<br>"
next
 
D

Dan

Adrienne Boswell said:
I have a form that on the front end Is dynamically generated by
drawing records from a database, then each record is displayed with a
checkbox next to it, where you can click multiple checkboxes, hit the
submit button in the form, and goes to a next page where it will
display the ID # of the record(s) you selected from the form page:
'''''''''''''
<form name="form1" action="postpage.asp" method="post"><input
type="checkbox" id="ID" name="ID" value="<%=RS("meID")%>">
''then the associated records are displayed inline, followed by submit
button
<input type="submit" name="go" id="go" value="GO" class="inputBox"/>

'''''''''''

works fine when selecting one record from the display; below is the
block of code that i am using to output all the ID#s from the form
data i selected:
<%
dim qid
qid = Request.Form("ID")

For each CheckedItem in Request.Form("ID")

response.write qid
next
%>
''''''''''''''''''''''''''''''''
if I select more than one record and hit submit, i will get the ID#s
of each, but i will also receive an extra figure displayed, which is
all the ID#s selected appended to each other. Ex:
1,
2
12
..and the first number will hav a comma appended to the output.
Womdering how to manage the "for each " loop in the code, or how am I
handling the event incorrectly?

That is the nature of the beast. The input type checkbox, because it
accepts multiple values, sends name/array pair instead of the usual
name/value pair. So what you want to do is treat what is coming back
from the checkbox request.form("id") as an array, eg:

dim idarr, i
idarr = split(request.form("id"),",")

for i = 0 to ubound(idarr)
response.write i & ": " & idarr & "<br>"
next



I'd advise against this - as splitting on the comma is not always reliable,
for instance if any of those fields allow a comma to be entered as text then
you'll be splitting that text into separate array keys, and losing the comma
itself. ASP already has a mechanism built in for handling the Request.Form
collection without having to resort to using Split and risking a mess.


For Each key In Request.Form
Response.Write key & ": " & Request.Form(key) & "<br>"
Next
 
D

Dan

Adrienne Boswell said:
I have a form that on the front end Is dynamically generated by
drawing records from a database, then each record is displayed with a
checkbox next to it, where you can click multiple checkboxes, hit the
submit button in the form, and goes to a next page where it will
display the ID # of the record(s) you selected from the form page:
'''''''''''''
<form name="form1" action="postpage.asp" method="post"><input
type="checkbox" id="ID" name="ID" value="<%=RS("meID")%>">
''then the associated records are displayed inline, followed by submit
button
<input type="submit" name="go" id="go" value="GO" class="inputBox"/>

'''''''''''

works fine when selecting one record from the display; below is the
block of code that i am using to output all the ID#s from the form
data i selected:
<%
dim qid
qid = Request.Form("ID")

For each CheckedItem in Request.Form("ID")

response.write qid
next
%>
''''''''''''''''''''''''''''''''
if I select more than one record and hit submit, i will get the ID#s
of each, but i will also receive an extra figure displayed, which is
all the ID#s selected appended to each other. Ex:
1,
2
12
..and the first number will hav a comma appended to the output.
Womdering how to manage the "for each " loop in the code, or how am I
handling the event incorrectly?

That is the nature of the beast. The input type checkbox, because it
accepts multiple values, sends name/array pair instead of the usual
name/value pair. So what you want to do is treat what is coming back
from the checkbox request.form("id") as an array, eg:

dim idarr, i
idarr = split(request.form("id"),",")

for i = 0 to ubound(idarr)
response.write i & ": " & idarr & "<br>"
next


Sorry, I misread your reply, I thought you were encouraging use of Split on
the entire Request.Form collection, not just the ID.

Anyway, my point still stands - don't use it for this either. It only works
if you don't have a comma possible in the value of the checkboxes (which
granted will be most of the time, but not necessarily 100% of the time).
Here's a method that works all the time:

For i = 1 To Request.Form("id").Count
Response.Write i & ": " & Request.Form("id")(i) & "<br>"
Next

Again, no need to Split into an array, the collection has already handled
all of this for you.

To demonstrate, given the following form:

<form action="http://www.compman.co.uk/vars.asp" method="POST">
<input type="checkbox" name="id" value="1,2"> 1,2<br>
<input type="checkbox" name="id" value="2,3"> 2,3<br>
<input type="checkbox" name="id" value="3,4"> 3,4<br>
<input type="checkbox" name="id" value="4,5"> 4,5<br>
<input type="submit">
</form>


If you were to tick all the boxes, the output of your suggestion gives:

0 : 1
1 : 2
2 : 2
3 : 3
4 : 3
5 : 4
6 : 4
7 : 5

which is obviously incorrect. Whereas my solution results in:

1 : 1,2
2 : 2,3
3 : 3,4
4 : 4,5

which is the expected result.
 
A

Adrienne Boswell

Sorry, I misread your reply, I thought you were encouraging use of
Split on the entire Request.Form collection, not just the ID.

Anyway, my point still stands - don't use it for this either. It only
works if you don't have a comma possible in the value of the
checkboxes (which granted will be most of the time, but not
necessarily 100% of the time). Here's a method that works all the
time:

For i = 1 To Request.Form("id").Count
Response.Write i & ": " & Request.Form("id")(i) & "<br>"
Next

Again, no need to Split into an array, the collection has already
handled all of this for you.

You are right. I forgot about that method. I've been authoring in PHP
- and getting stuck in this very same quagmire.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top