Count number of POST variables?

I have a form that has forty checkboxes in it and I want to count how many are checked when the form is submited.

What is the best way of doing this?

 
$total =""
 
if(isset($_POST['cb1'])){
//count it
$total = $total + 1;
}
if(isset($_POST['cb2'])){
// add it to the total
$total = $total + 1;
}
//etc 

Maybe this will work:


foreach($_POST as $k => $v){
  ++$i; // note I'm using ++$i instead of $i++, the one I'm using is faster
}
if($i < 40){
  echo 'Please enter all fields!';
}

sorry, perhaps I didn’t give enough information

I don’t need all the checkboxes selected. I just want to count how many are selected.

I also have other form elements (text fields, select boxes, other check boxes etc) being sent at the same time which I don’t want to count.

At the moment I’m using the code I posted above repeated 40 times to count it but I thought there may be a better (less code) way.

Oh, I misread, you’re right. What you would have to do is give each checkbox name a name with the prefix “_c” or something similar. Then you can use the top half of my code and then echo $i :slight_smile: Of course, first do a [fphp]preg_match[/fphp] to see if it is actually a checkbox (and thus contains the “_c” prefix).

It just so happens all the checkboxes I want to count all start with cb. And are numbered cb1, cb2, cb3…etc

I’m not too sure how to put that into your code??


foreach($_POST['cb'] 
 
//or
 
foreach($_POST['cb$']
 
//??????????
 

Here’s an example piece of code:


foreach($_POST as $k => $v){
  if(preg_match('#cb#i', $_POST[$k]){ // the i after delimiter means case-insensitive
    ++$i; // note I'm using ++$i instead of $i++
  }
}

You could make your checkboxes you want counted part of an array - arrays also work in html forms, <input type=“checkbox” name=“myarray[value1]” value=“ischecked”>, <input type=“checkbox” name=“myarray[someothervalue]” value=“ischecked”>, etc -
On the action page,
$countarray = array_count_values($_POST[‘myarray’]);
/* $myarray[‘ischecked’] now equals the number of boxes that were checked */

array_count_values is indeed a great function, I had forgotten about it. :smiley:

For some reason it always counted one more than it was supposed to so I just added


$i = $i -1;


foreach($_POST as $k => $v){ 
if(preg_match('#ad#i', $_POST[$k])){ // the i after delimiter means case-insensitive 
	++$i; // note I'm using ++$i instead of $i++ 
$i = $i-1;
} 
} 

I hope that was the right thing to do? If it is, thanks for your help :slight_smile:

If the code works, then it’s the right thing to do :slight_smile:

However, minck’s code might be faster, so you would have to test that if you’re worried about speed.

Btw, no problem :slight_smile:

<input type="checkbox" name="name[]" value="1">

foreach($_POST["name"] as $value) {
$var .= count($value);
}
echo $var;

kilroy…
where u get the $i var if u use foreach statement???
cheers :slight_smile:

Let’s say you have something like this.


<input type="checkbox" name="c[1]" value="1" /> Checkbox 1
<input type="checkbox" name="c[2]" value="1" /> Checkbox 2
<input type="checkbox" name="c[3]" value="1" /> Checkbox 3
etc

This sounds simple enough.


echo count($_POST['c']);

I’m not 100% certain this will work, but you should try it.

mmj’s solution is the easiest to implement and works!
(It’s similar to the code I use!)

You could do this too - but first you’d want to expunge all the null values. When forms come in, the fields that are empty result in variables that are set, but empty, and count doesn’t discriminate between empty and non-empty elements - they’re all ‘counted’.
<edit> no - this isn’t true - unchecked checkboxes don’t come in as null fields - method seems fine as is - my mistake (and I’ve been overcoding my forms all this time!)</edit>

You’d have to initialize that before the foreach loop ($i = ‘’) However, the method posted by minck is a lot easier to use, so I’d recommend that :slight_smile:

Bummer!

Seriously it is this easy…


<input type="checkbox" name="name[]" value="1">
<input type="checkbox" name="name[]" value="2">
<input type="checkbox" name="name[]" value="3">
<input type="checkbox" name="name[]" value="4">
<input type="checkbox" name="name[]" value="5">
<input type="checkbox" name="name[]" value="6">
<input type="checkbox" name="name[]" value="7">
<input type="checkbox" name="name[]" value="8">
<input type="checkbox" name="name[]" value="9">
<input type="checkbox" name="name[]" value="10">
<input type="checkbox" name="name[]" value="11">

# after submit
$count_cbox = count($_POST['name']);