It seems that a lot of people run into this issue in regards to dealing with the result that checkboxes return to the Request.Form object. So this article is intended to fully explain how checkboxes are interpretted, what their values are and aren't, and how they should be handled if inserted into your database as a yes/no or bit field.
Most commonly, I see people trying to check a checkbox to see if it has a True or False value. This is incorrect. Checkboxes do
not return a boolean value. They return, by default, a value of "on" if they are checked, and do not return a value if they are not checked. If you set the
value attribute of a checkbox, the checkbox will return that value if it is checked instead of "on", but it still will not return a value if it is not checked.
Example:
-
<input type="checkbox" name="mycheckbox" id="mycheckbox"/>
-
<!--Will return a value of "on" if it is checked, and will return EMPTY or "" if it is not checked -->
-
-
<input type="checkbox" name="mycheckbox" id="mycheckbox" value="MyNameIsMark"/>
-
<!--Will return a value of "MyNameIsMark" if it is checked, and will return EMPTY or "" if it is not checked.-->
-
-
If Request.Form("mycheckbox") = "MyNameIsMark" Then
-
Response.Write "My Check Box has been checked!"
-
Else
-
Response.Write "No one wants to check my checkbox :("
-
End If
-
Inserting the checkbox into your database
Many people also incorrectly update their database. Especially when it comes to access.
If you are using access, a yes/no field should be supplied a value of -1 for yes, and 0 for no.
If you are using SQL Server, a bit field should be supplied a value of True or False, respectively.
I would recommend setting the default value of your yes/no or bit fields with 0 (access) or False (SQL Server). If you do this, you will not have to worry about setting the value of your bit or yes/no field to false in your script everytime you insert. Unless you specifically tell your database to set the value of your yes/no or bit field to Yes/True/-1 etc - it will automatically set itself to No/False/0 etc when a new record is inserted.
-
'Access
-
If Request.Form("Checkbox") = "on" Then
-
rs("Yes-No") = -1
-
End If
-
-
'SQL Server
-
If Request.Form("Checkbox") = "on" Then
-
rs("Yes-No") = True
-
End If
-
Mark