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!';
}
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 Of course, first do a [fphp]preg_match[/fphp] to see if it is actually a checkbox (and thus contains the “_c” prefix).
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 */
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
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>