View Full Version : passing checkbox form values help, having issues with passing non-checked value

Dec 5th, 2011, 10:41 AM
Hello, i'm working on a 3 page survey. When hitting next, previous, or submit it passes the values of all the questions to the next page. I've got the whole thing working correcting except for one thing: When the box is "not" checked it passes no value. I'm needing it to have a value of "1" when checked and a value of "0" when not checked, and currently when its not checked and i pass the info it leaves it blank.

I'd post the whole code to one of the pages but it's long , so i'll post the snipits of the code.

<script type="text/javascript">
/* <![CDATA[ */
function processQueryString() {
var formData = location.search;
formData = formData.substring(1, formData.length);
while (formData.indexOf("+") != -1) {
formData = formData.replace("+", " ");
formData = unescape(formData);
var formArray = formData.split("&");
for (var i=0; i < formArray.length; ++i) {
//document.writeln(formArray[i] + "<br />");
var sDataName = formArray[i].split("=")

switch (sDataName[0]) {

case ".lib_use":
for (var j=0; j < document.getElementsByName(".lib_use").length; ++j) {
if (document.getElementsByName(".lib_use").item(j).value == sDataName[1]) {
document.getElementsByName(".lib_use").item(j).checked = true;
//alert("lib_use set");
case ".lib_comp":
if (sDataName[1] == 1) {
document.getElementsByName(".lib_comp").checked = true;
document.getElementsByName(".lib_comp").value= 1;
else {
document.getElementsByName(".lib_comp").checked = false;
document.getElementsByName(".lib_comp").value= 0;
alert("not caught = " + sDataName[0]);

/* ]]> */

<input type="checkbox" name=".lib_comp" id="lib_comp" value="1" />

The first case that i showed in my code is a radio button, and it passes correctly, i just wanted to show the "format" i was using in a working sense. The 2nd case is an example of the check boxes.

Thanks for looking at this, and giving any suggestions you might have!

Dec 5th, 2011, 10:57 AM
use an auxiliary hidden input (type="hidden") and pass the name of the checkbox to that hidden input. Of course, you need to change the javascript code of the function processQueryString() accordingly

<input type="checkbox" onclick="lib_comp.value=this.checked?'1':'0';" />
<input type="hidden" value="0" name="lib_comp" />

Anyway, you have two fundamental errors in your code:

1. name and id must start with a letter, not with a dot, thus name=".lib_comp" is not valid
See also:

2. the method document.getElementsByName() returns always a collection (like an array), thus you should specify the index even if you have a single control. Like:


Dec 5th, 2011, 11:18 AM
yeah i know the . thing, but it's the way the database is reading the file. The names and id's were already specified for me.

However: THANK YOU. The hidden field worked. Thank you SO much i'd never thought about the over riding nature of the name. It worked beautifuly. So thanks again!

Old Pedant
Dec 5th, 2011, 10:30 PM
The other way to do this may actually be simpler:

function cleanUp(form)
var inps = document.getElementsByTagName("input");
for ( var i = 0; i < inps.length; ++i )
var inp = inps[i];
if ( inp.type.toLowerCase() == "checkbox" && inp.checked == false && inp.value == "1")
inp.value = "0";
inp.checked = true;
return true;
<form ... onsubmit="return cleanUp(this);">
<input type="checkbox" name="anything" value="1" />
<input type="checkbox" name="whatever" value="1" />

See? When the form is submitted, you go find all checkboxes that are unchecked and have a value of 1. You change their value to 0 and check them. *NOW* they *ARE* sent on to the next page.

I find this simpler than the hidden field trick. But each to his/her own.

Old Pedant
Dec 5th, 2011, 10:31 PM
You could instead give a special class="mustBeChecked" to only those checkboxes that you want this to happen to and then look for the inp.className in the loop instead of value==1.