Can a checkbox in an html form send one value to the server when checked, and a different value when not checked? So far, it looks like to me that when not checked, the only choice is to send nothing to the server when you post the form.
What I would like is the checkbox to post something like “yes” when checked, and “no” when not checked. Or any two values; True/False, on/off, etc.
I’m trying to make some changes to a website that someone else designed. And although I’m a programmer, I’m a novice web programmer at best.
The site is done in a combination of html and asp pages. They have a dozen or so forms that we need to save the contents of and allow certain users to display the form later, showing all the data as it was filled in.
The forms are currently posting to an asp script, which is using the request.form collection to save the data as a comma delimited set.
The code in the asp script looks something like this:
InputFieldCount = Request.Form.Count For i = 1 To (InputFieldCount) FormsText = FormsText & request.form(i) & strQuote next
This collects all the data posted from the form in the “FormsText” variable, which winds up looking something like this:
“data from form field 1”,”data from form field 2”,……”data from last field”
This then gets saved in a varchar field in the database (MS SQL), pulled out later and displayed.
The problem is with checkboxes. If the checkbox is checked, whatever you specified as the value for the checkbox appears in the request.form collection just as it should. But if the checkbox is not checked, you don’t get a null in that position, that field is not included in the collection at all, and the Request.Form.Count is lower by 1. It is as if the checkbox doesn’t exist.
Needless to say this makes it difficult later to put the data back into a form by putting the first item in the comma delimited set into the first field on the form, the second item in the set into the second field and so on. The missing checkbox makes everything “slide up” one position, and the comma delimted set no longer lines up with the fields in the form.
So given all that, is there a way to make checkboxes post a value when not checked, as well as when checked?