setting default html checkbox values?

butopia
butopia used Ask the Experts™
on
I have an html form with a series of checkboxes.
If I have a field in an Access DB with a data type of yes/no, how do i set up the values on the html form.

Currently, the checkbox (if checked) returns "true" (and this shows up correctly in the Access DB)
but the ASP page blows up if the box is not checked. I suppose this is becaue the value is "blank" so the update doesn't work. Is there a way to set a default value of "false" that would be posted to the asp page (to then be appended to the access db) UNLESS the box is checked (then "true" would be posted).
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
unchecked checkboxes do not get submitted when posting a form;

check for the existance of the control and set a variable to its value if exists, if not set it to the "false"

ei:

vbscript ---

dim boolMyCheckBoxValue

if request.form("chkMyCheckBox") = "" then
  boolMyCheckBoxValue = False
else
  boolMyCheckBoxValue = True
end if

'or for string values - assume the checkbox value="1"
dim strMyCheckBoxValue

if request.form("chkMyCheckBox") = "" then
  strMyCheckBoxValue = "0"
else
  strMyCheckBoxValue = request.form("chkMyCheckBox")
end if

thomasdodds
then send your variable to the database ...
often I have used the combination of hidden form fields with the unselected value and then changed it with the onclick of the check box: the hidden fields will always be populated and submitted.

something like this....

<input type=checkbox id=chk1 name=chk1 onclick="javascript:doSethidden(this.id,1)">
<input type=hidden id=1 name=1 value="false">

<!-- just increment the numbers for more -->

<script language=javascript>
<!--
 function doSetHidden(sender,field)
 {
  var f = document.forms[0]
  //use whatever index your form is if you have more than one
  if(f.sender.checked)
  {
    f.field.value = 'true';
  }
  else
  {
    f.field.value = 'false';
  }
 }
//-->
</select>
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

var f = document.forms[0]
 should read
var f = document.forms[0];

sorry...
Top Expert 2013

Commented:
or change the check boxes to radios:

yes <input type="radio" name ="r1" value="true"> no <input type="radio" name="r1" checked value="false">


Now r1 will always return true or false.

Cd&

Author

Commented:
Yes, I really wish it was as simple as radio buttons, but this is a long list of interests and having something like

Music Interests
rock n' Roll    Y     N
Classical       Y     N
soft Rock       Y     N

Seems a little cluttered, but a good alternative suggestion.
you could do it with select boxes as well; that way you would only have one control and a 'default' response;

<SELECT id=cboRockRoll name=cboRockRoll>
  <OPTION value="N" SELECTED>No</OPTION>
  <OPTION value="Y">Yes</OPTION>
</SELECT>

Author

Commented:
Very elegant solution with the hidden aspect. Mucho gracias!
not a problem, glad I could help

thomasdodds

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial