开发者

Count number of Checked check boxes from a php gathered check list

开发者 https://www.devze.com 2023-03-23 01:37 出处:网络
I\'m presented with a problem using PHP and MYSQL. I have a dynamic list of options which the user can select (maximum of 3) that are added from the administration panel as shown below:

I'm presented with a problem using PHP and MYSQL. I have a dynamic list of options which the user can select (maximum of 3) that are added from the administration panel as shown below:

<input type="checkbox" name="<?php echo "category".$i; ?>" 
   value="<?php echo $cat_id; ?>" />
  <?php echo $cat_name; ?><br />
<?php
$i++;
}
echo "<input type='hidden' name='num_cat' value='$num_cat' />";
?>

I now want to count how many check boxes are 'checked' and if there are more than 0 and less than 4 checked it will update the mysql table with these stored. They are stored by means of 1's and 0's. So they tick 'y开发者_如何学JAVAes' and a 1 is stored, they tick 'no' and a 0 is stored.

I've been trying to use jQuery and Javascript but they all seem to be for Check Box Forms which have the values pre-written within a form, mine are dynamic from a database.

Many thanks for your help.


Use [] in the end of your checkbox names and use the sizeof function on the corresponding $_POST[] array in your php script.

<input type="checkbox" name="values[]" value="1" />Value 1
<input type="checkbox" name="values[]" value="2" />Value 2

sizeof($_POST['values']);

Note that the checkboxes have the same name and end with brackets (values[]). This indicates that the checkboxes belong together and are bundled as an array in php. Only the selected values will be present in the array as well.

0

精彩评论

暂无评论...
验证码 换一张
取 消