Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

counting how many checkbox are checked php/html

Tags:

html

checkbox

php

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"/>
like image 616
user2732815 Avatar asked Nov 30 '22 12:11

user2732815


2 Answers

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.

like image 114
Gautam3164 Avatar answered Dec 05 '22 23:12

Gautam3164


$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++;
}
like image 33
Praind Avatar answered Dec 06 '22 00:12

Praind