Adding checkboxes

OK, I am a newbe

Say I have a bunch of checkboxes

 <tr>
   <td colspan='2' class='normal'><input type='checkbox' name='formCheck1'>Check If this Form is Correct</td>
 </tr>
 <tr>
   <td colspan='2' class='normal'><input type='checkbox' name='formCheck2'>Check If this Form is Correct</td>
 </tr>
  <tr>
   <td colspan='2' class='normal'><input type='checkbox' name='formCheck3'>Check If this Form is Correct</td>
 </tr>
  <tr>
   <td colspan='2' class='normal'><input type='checkbox' name='formCheck4'>Check If this Form is Correct</td>
 </tr>

And in a function I want to add the number of checkboxes checked.

I am looking for some kind of hack.
I would rather not have to do this over and over

if(!formname.formCheck1.checked)
{
}

I have tried

x = x + ormname.formCheck1.checked

Hoping that a true might work as a 1.

Does anyone know an easy way for this to be done?

thanks

Well, you could use an array for the checkboxes. It works very easy.

So, instead of this: name=‘formCheck1’ and the others, use an array.
Something like this: name=‘formCheck[1]’ This creates an array of checkboxes…

What this does is each checkbox will be added to this array. So, formCheck1 will really be formCheck[0]…

So, to count them you can just check them in a loop like this:
$checkCount = count($_POST[‘formCheck’];
Remember the formCheck is now an array. All CHECKED boxes are in it. All UNCHECKED boxes are NOT in it.
SO, the count is just however many boxes have been checked.

You can still use them individually if needed with $somevariable = $_POST[‘formCheck[3]’];
(But, why would you?) Lastly, you can use a FOR or FOREACH loop and parse thru them if needed…

Hope that helps! Good luck…

Thanks for the help

Let me post a simple example

On this I am not getting anything alerting.
Any ideas?
thanks

<script type="text/javascript">
<!--

function doIt()
{

   $checkCount = count($_POST['music']);   
alert ($checkCount);
}

//-->
</script>


<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=windows-1250">
  <meta name="generator" content="PSPad editor, www.pspad.com">
  <title></title>
  </head>
  <body>
<form name="orderform">
What kind/s of music do you listen to?<br>
<input type="checkbox" name="music[1]" value="Rock" checked="checked">
Rock<br>
<input type="checkbox" name="music[2]" value="Reggae">
Reggae<br>
<input type="checkbox" name="music[3]" value="Pop">
Pop<br>
<input type="checkbox" name="music[4]" value="Rap">
Rap<br>
<input type="checkbox" name="music[5]" value="Metal">
Metal<br>
<input type="submit" onclick="doIt()">
</form>
  </body>
</html>

LOL… You are mixing your programming languages…

You are using a HTML FORM which is used to input the checkboxes. Then, you would normally “POST” this form to a PHP file or whatever. But, in your code, you are using Javascript to handle the data…

When using Javascript, you can not access the fields inside of a form directly as you would with HTML or PHP.
You must use the document level commands instead. Therefore, the JS code for this:
$checkCount = count($_POST[‘music’]);
alert ($checkCount);

Should be something like this:
var checkcount = document.getElementsByName(‘music[]’);
alert(checkcount.length);

What this does is pull all of the checkboxes using Javascript into an array variable and then displays the length of the array. In JS, a “length” of an array is the count of items in it. Hope that helps…

Sponsor our Newsletter | Privacy Policy | Terms of Service