PDA

View Full Version : Check box form


robert475
11-24-2009, 12:55 PM
Hi

I have a form with a few check boxes where the value is populated from the database e.g

<code>
<input type="checkbox" name="multiple" value="& productsRs("productCode") &" />
</code>

I have say around five of these. I obtain the values of the checkboxes like this:

<code>
Dim Item

For Each Item In Request.QueryString("multiple")
Response.Write Item & " "
Next
</code>

The page will then display all the values of the checkboxes.

The part I am stuck on is how do I separate the values of each checkboxes into session variables after the for each statement e.g:

checkbox value1 = session("1")
checkbox value2 = session("2")
checkbox value3 = session("3")
checkbox value4 = session("4")

Any suggestions please?

Old Pedant
11-24-2009, 06:30 PM
It doesn't work like that.

The problem is that an *UN*checked checkbox sends *nothing at all* from the browser to the server. (HTTP spec, nothing to do with ASP, per se.)

So you can't depend on the 1st value in the FOR EACH being the value from the first checkbox. If you have 5 checkboxes and the user only checks the last one, then *that* value will be the first (and only).

Tell us what the checkboxes are for and/or what you want to do with the data and let's come up with a different way to do this.

robert475
11-25-2009, 01:20 PM
What I am trying to do is compile a 'compare product' feature in the products section of the website. The user can select up to a maximum of three checkboxes (products), when they hit the submit button the relevant product spec is displayed. The value of each checbox is dynamic as it is driven from the database.

Hope this gives you a better idea of the output.

Old Pedant
11-25-2009, 08:13 PM
So I don't understand why you need to do anything *outside* of the FOR EACH.

Why can't you just do
<code>
Dim Item

For Each Item In Request.QueryString("multiple")
... show all the info about this item ...
Next

But yeah, you could save them all in an array, pretty much automatically:

itemArray = Split( Request("multiple"), ", " ) ' split on COMMA-SPACE
Session("chosenCheckboxes") = itemArray
For i = 0 To UBound(itemArray)
item = itemArray(i)
... do whatever with item ...
Next

And the thing is, you can do that same for loop on another page with that same array:

itemArray = Session("chosenCheckboxes")
For i = 0 To UBound(itemArray)
item = itemArray(i)
... do whatever with item ...
Next

But...

But I'm not at all clear that you should ever create the separate items or the array. To get all the products that were checked from the DB, you really only need to do:

SQL = "SELECT * FROM products WHERE productID IN ( " & Request("multiple") & ") "
...