Reputation: 29533
I have a checkbox
<td><strong>Online Ordering: </strong></td>
<td><input type="checkbox" name="OnlineOrdering" value="<%=OnlineOrdering%>" <% if OnlineOrdering = True then response.write "checked='Checked'" end if %>/></td>
How do i capture whether the checkbox is checked or unchecked when form is submitted?
OnlineOrdering = request.form("OnlineOrdering")
this does not work?
Upvotes: 0
Views: 13834
Reputation: 97
I found this post because I was trying to solve the same problem. I have a solution that seems to work well.
In the HTML form, create dynamically generated checkboxes. The value of this_box_id used below can be any unique number. In my case it was the primary key (autonumber) for that question from the SQL Database . Dynamically generated the check boxes with an associated hidden field:
<input type="hidden" name="check_box_indicator" value="<%=this_box_id%>">
<input type="checkbox" name="box_id<%=this_email_selection_id%>"
value="<%=this_box_id%>">
When asp returns the values for this, it will return multiple values for check_box_indicator. Here's a sample query string snippet:
...&check_box_indicator=78&box_id78=78&check_box_indicator=98&check_box...
On the next page, ASP will read through the form data. It will find EVERY check_box_indicator and it's value. That value can be used to check the value of the associated checkbox. If that checkbox comes back, it was checked, if it doesn't you will know that it wasn't checked. In the sample above, checkbox 78 was checked and so the box_id78 value was passed, while checkbox 98 was not and box_id98 was not sent. Here's the code to use this.
For Each this_box_id In Request.Form("check_box_indicator") 'check EVERY box'
box_state = Request.Form("box_id"&this_box_id) 'collect passed values only'
if box_state > 0 then
store_value = "y"
else
store_value = "n"
end if
' you now have the this_box_id number identifying this item in the DB, '
' the value of the check box, if it was passed '
' and a definite y or n value '
Next
With the example querystring, you will get 78 y, 98 n
Upvotes: 2
Reputation: 2472
This should assign a true/false to the variable OnlineOrdering:
If Request.ServerVariables("REQUEST_METHOD") = "POST" Then
OnlineOrdering = (Request.Form("OnlineOrdering") <> "")
End If
Upvotes: 2