Log in

View Full Version : Count checkbox



rocg23
11-14-2006, 09:35 PM
Is there a way to count the number of check boxes that are checked, while they are still putting information into a database?

I have an order form, with options on it, but each option needs to increas the total price. I need to record the amout of options, and post the options in the database. I can post the array, but don't have a clue on how to do the count.

Thanks...

Acey99
11-15-2006, 12:01 AM
that's a JavaScript Question

and the answer is YES

with JS:



<script language="JavaScript" type="text/javascript">
<!--
function CountCheck(){
var chkCnt = 0;
var Frm = document.forms[0]; // or name where the first form is 0
var dLen = Frm.length - 1;
for (i=0;i<=dLen;i++){
if (Frm.type == 'checkbox') chkCnt++;
}
} // end function
//-->
</script>

djr33
11-15-2006, 12:22 AM
With PHP, you could check that too.

I can't remember the value sent via POST/GET to PHP, but I think it's 1. Maybe TRUE.

Anyway, you could search through the entire post/get array for any "1" values but that would include ANY, not just the checkboxes.

Really, you would need to just check each individually and for each add 1 to $count or something.

And, if you need this in realtime without submitting the form, then you would need to use Javascript.

mwinter
11-15-2006, 01:39 AM
I can't remember the value sent via POST/GET to PHP, but I think it's 1. Maybe TRUE.

The value of the control. All form controls send the current value (the value property via the DOM), paired with the control name.

If the control names are the same for all related checkboxes - and they often should be - then those names should end with a pair of square brackets: []. PHP will then construct an array of these values, and the length of that array will be the number of checked checkboxes.

For example, with a group of checkboxes (or any control, for that matter) named "options[]" (name="options[]") then there would be an array named "options" in the $_GET or $_POST superglobals (depending on the method used to submit the form, obviously). If no checkboxes were checked, then no element would exist. That is,



isset($_POST['options'])

would return false.

Mike

djr33
11-15-2006, 02:30 AM
But how would you check how many? The question isn't checking if none exist, though that is interesting.

rocg23
11-15-2006, 01:01 PM
that's a JavaScript Question

and the answer is YES

with JS:



<script language="JavaScript" type="text/javascript">
<!--
function CountCheck(){
var chkCnt = 0;
var Frm = document.forms[0]; // or name where the first form is 0
var dLen = Frm.length - 1;
for (i=0;i<=dLen;i++){
if (Frm.type == 'checkbox') chkCnt++;
}
} // end function
//-->
</script>



Cool...thanks.

I really don't know JS at all, but this will work if I know the variable I am looking for. Is $i my ouput?

mwinter
11-15-2006, 04:07 PM
But how would you check how many?

I already answered that: the length of the array (if it exists) represents the number of checked checkboxes.



$checkboxCount = isset($_POST['options']) ? count($_POST['options']) : 0;


If the checkboxes don't have a similar array-like control name[1], things become harder. One solution is to list the names in an array, then loop through them, checking if an element exists:



$checkboxes = array('foo', 'bar' /* ... */);
$checkboxCount = 0;

foreach ($checkboxes as $name) {
if (isset($_POST[$name])) ++$checkboxCount;
}





I really don't know JS at all, but this will work if I know the variable I am looking for. Is $i my ouput?

If you're doing this server-side - I assume you are due to database interaction - then client-side scripting is useless at this stage.

Mike


[1] As well as naming form controls with a trailing, empty bracket pair, those brackets can contain a value. The array will still be created as before, but rather than having numeric indices, results can be accessed by name. Consider:



<form action="..." method="post">
<fieldset>
<legend>Personal details</legend>
<label>First name:
<input name="personal[first-name]" type="text" value=""></label>
<label>Last name:
<input name="personal[last-name]" type="text" value=""></label>
</fieldset>
</form>

When submitted, the $_POST superglobal will contain an element named 'personal'. This will be an array with two elements, 'first-name' and 'last-name'.

Acey99
11-15-2006, 05:41 PM
in regards to rocg23 on the JS output.

chkCnt is the number of checkboxes.

yes PHP can do it as well, but sometimes why?

USING $_POST[] or $_GET (Depends on what <form Method="" is

count($_POST["checkBoxName]);
or
count($_GET["checkBoxName]);

also need to at least have one checkbox checked in this case if using server side.

mwinter
11-15-2006, 06:14 PM
chkCnt is the number of checkboxes.

A value that is inaccessible because it is local and never returned.



yes PHP can do it as well, but sometimes why?

In this instance, because the information is necessary server-side (assuming I understand the OP correctly), and there is no benefit to calculating it client-side.

Sites should not rely on client-side scripting. A site should work with only server-side support in the first instance. Client-side code can then be added where it helps the user or makes actions more efficient. Even if something is engineered such that the client does all of the work, if that feature is important (and here is surely is), then a server-side fall back is required.



count($_POST["checkBoxName]);
or
count($_GET["checkBoxName]);

also need to at least have one checkbox checked in this case if using server side.

No, you don't. I already posted how to do this.

Mike

rocg23
11-15-2006, 06:58 PM
[QUOTE=mwinter;61706]I already answered that: the length of the array (if it exists) represents the number of checked checkboxes.



$checkboxCount = isset($_POST['options']) ? count($_POST['options']) : 0;



Thanks mwinter...This did exactly what I needed it to do...returned the number checked, and I verified that it came back 0 with nothing checked.