Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP General > processing mutiple checkbox selections in vbscript

Reply
Thread Tools

processing mutiple checkbox selections in vbscript

 
 
.Net Sports
Guest
Posts: n/a
 
      09-24-2009
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?
 
Reply With Quote
 
 
 
 
Adrienne Boswell
Guest
Posts: n/a
 
      09-25-2009
Gazing into my crystal ball I observed ".Net Sports"
<(E-Mail Removed)> writing in news:cd2e0629-858a-4b8e-8e4e-
http://www.velocityreviews.com/forums/(E-Mail Removed):

> 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[i] & "<br>"
next


--
Adrienne Boswell at Home
Arbpen Web Site Design Services
http://www.cavalcade-of-coding.info
Please respond to the group so others can share

 
Reply With Quote
 
 
 
 
Dan
Guest
Posts: n/a
 
      09-29-2009

"Adrienne Boswell" <(E-Mail Removed)> wrote in message
news:Xns9C90B98EF72A3arbpenyahoocom@188.40.43.213. ..
> Gazing into my crystal ball I observed ".Net Sports"
> <(E-Mail Removed)> writing in news:cd2e0629-858a-4b8e-8e4e-
> (E-Mail Removed):
>
>> 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[i] & "<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


--
Dan

 
Reply With Quote
 
Dan
Guest
Posts: n/a
 
      09-29-2009

"Adrienne Boswell" <(E-Mail Removed)> wrote in message
news:Xns9C90B98EF72A3arbpenyahoocom@188.40.43.213. ..
> Gazing into my crystal ball I observed ".Net Sports"
> <(E-Mail Removed)> writing in news:cd2e0629-858a-4b8e-8e4e-
> (E-Mail Removed):
>
>> 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[i] & "<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.

--
Dan

 
Reply With Quote
 
Adrienne Boswell
Guest
Posts: n/a
 
      09-29-2009
Gazing into my crystal ball I observed "Dan" <(E-Mail Removed)>
writing in news:(E-Mail Removed):

> 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.

--
Adrienne Boswell at Home
Arbpen Web Site Design Services
http://www.cavalcade-of-coding.info
Please respond to the group so others can share

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Stop browser from remembering checkbox selections Laser Lips HTML 3 02-23-2010 05:18 PM
How to persist checkbox selections across recordset pages Parag Gaikwad ASP General 18 07-26-2007 04:10 PM
Checking selections made in a checkbox list Menelty Javascript 2 06-11-2004 09:12 PM
Runtime Datagrids and checkbox selections DJ Dev ASP .Net 1 02-10-2004 04:16 AM
Runtime Datagrids and checkbox selections DJ Dev ASP .Net Datagrid Control 1 02-10-2004 04:16 AM



Advertisments