Counting the number of flags checked by php / html

Hi, I am new to php, and I was wondering how I can calculate how many 'checkbox' checked as soon as I hit the submit button. For instance:

 <input type = "checkbox" value = "box" name = "checkbox1"/> <input type = "checkbox" value = "box" name = "checkbox2"/> <input type = "checkbox" value = "box" name = "checkbox3"/> 
+4
source share
7 answers

Give label names as an array, e.g.

 <input type = "checkbox" value = "box" name = "checkbox[]"/> 

And after submit try

 $checked_arr = $_POST['checkbox']; $count = count($checked_arr); echo "There are ".$count." checkboxe(s) are checked"; 

Note. . And based on the method your form submits with ..., whether $_GET or $_POST , you need to use $_POST['checkbox'] for POST and $_GET['checkbox'] for the GET method.

+12
source
 <input type = "checkbox" value = "box" name = "checkbox"/> <input type = "checkbox" value = "box" name = "checkbox"/> <input type = "checkbox" value = "box" name = "checkbox"/> 

to check which fields were checked, just go through the chk [] array as follows:

 $chk_array = $_POST['checkbox']; for($chk_array as $chk_key => $chk_value) { print 'Checkbox Id:'. $chk_key . ' Value:'. $chk_value .'is checked'; } 
+1
source

Using jQuery, you can achieve it:

$("input:checkbox:checked").length

This will return the number of checks for checboxes.

And in php you need to pass it as an array.

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

0
source
 $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++; } 
0
source

You can set the names for the flags as an array:

 <input type = "checkbox" value = "box" name = "checkbox[1]"/> <input type = "checkbox" value = "box" name = "checkbox[2]"/> <input type = "checkbox" value = "box" name = "checkbox[3]"/> 

and then you will have an array in PHP ( $_POST['checkbox'] ):

 echo count( $_POST['checkbox'] ); // this will give you the count 

otherwise, you can iterate over each of them and increase the variable:

 $counter = 0; foreach( array('checkbox1', 'checkbox2', 'checkbox3') as $name ) { if( isset( $_POST[ $name ] ) { $counter++ } } echo $counter; 
0
source

You need to rename names and add values

 <input type = "checkbox" value = "box" name = "checkbox[]" value="1"/> <input type = "checkbox" value = "box" name = "checkbox[]" value="2"/> <input type = "checkbox" value = "box" name = "checkbox[]" value="3"/> 

This way you will learn not only the number (which you really do not need)

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

but also have the actual values โ€‹โ€‹selected:

 foreach($_POST['checkbox'] as $val) { echo "$val<br>\n"; } 
0
source

All the checkboxes that have been checked will be in the request when you click submit. In your case, if checkbox1 is checked, you will get: "CheckBox1 = window"

If you use GET as a method, it will look like this: http://yoururl.com/yourcode.php?checkbox1=box , and you can access it $ _GET ['CheckBox1']

If you use POST as a method, you can access it with $ _POST ['checkbox1']

You can also check if the checkbox (and in the request data) is checked with isset ($ _ POST ['checkbox1'])

-one
source

Source: https://habr.com/ru/post/1500132/


All Articles