Hi I am new with php and I was wondering how am i able to count how many 'checkbox'
are checked once I click on submit.
For example:
<input type = "checkbox" value = "box" name = "checkbox1"/>
<input type = "checkbox" value = "box" name = "checkbox2"/>
<input type = "checkbox" value = "box" name = "checkbox3"/>
Give the checkboxes names as array like
<input type = "checkbox" value = "box" name = "checkbox[]"/>
And after submit try like
$checked_arr = $_POST['checkbox'];
$count = count($checked_arr);
echo "There are ".$count." checkboxe(s) are checked";
Note : And based on the method that your form submit using...whether it is $_GET
or $_POST
you need to use $_POST['checkbox']
for POST method and $_GET['checkbox']
for the GET method.
$checkedBoxes = 0;
// Depending on the action, you set in the form, you have to either choose $_GET or $_POST
if(isset($_GET["checkbox1"])){
$checkedBoxes++;
}
if(isset($_GET["checkbox2"])){
$checkedBoxes++;
}
if(isset($_GET["checkbox3"])){
$checkedBoxes++;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With