计算有多少复选框被选中 php/html
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18572351/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
counting how many checkbox are checked php/html
提问by user2732815
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:
嗨,我是 php 新手,我想知道'checkbox'
一旦我点击提交,我怎么能计算出检查了多少。例如:
<input type = "checkbox" value = "box" name = "checkbox1"/>
<input type = "checkbox" value = "box" name = "checkbox2"/>
<input type = "checkbox" value = "box" name = "checkbox3"/>
回答by Gautam3164
Give the checkboxes names as array like
将复选框名称指定为数组
<input type = "checkbox" value = "box" name = "checkbox[]"/>
And after submittry 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 POSTmethod and $_GET['checkbox']
for the GETmethod.
注:基于该方法,你的表单提交使用......无论是$_GET
还是$_POST
你需要使用$_POST['checkbox']
的POST方法$_GET['checkbox']
的GET方法。
回答by Your Common Sense
You have to rename the 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 know not only number (which you don't actually need)
这样你不仅会知道数字(你实际上并不需要)
echo count($_POST['checkbox']);
but also have the actual selected values:
但也有实际选择的值:
foreach($_POST['checkbox'] as $val)
{
echo "$val<br>\n";
}
回答by Naveen Kumar Alonekar
<input type = "checkbox" value = "box" name = "checkbox"/>
<input type = "checkbox" value = "box" name = "checkbox"/>
<input type = "checkbox" value = "box" name = "checkbox"/>
to check which boxes have been checked simply traverse through the chk[] array like this:
要检查哪些框已被选中,只需像这样遍历 chk[] 数组:
$chk_array = $_POST['checkbox'];
for($chk_array as $chk_key => $chk_value)
{
print 'Checkbox Id:'. $chk_key . ' Value:'. $chk_value .'is
checked';
}
回答by Teneff
You can set names for the checkboxes 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']
):
然后你将在 PHP ( $_POST['checkbox']
) 中有一个数组:
echo count( $_POST['checkbox'] ); // this will give you the count
otherwise you can iterate over each one of them and increment a variable:
否则,您可以遍历它们中的每一个并增加一个变量:
$counter = 0;
foreach( array('checkbox1', 'checkbox2', 'checkbox3') as $name ) {
if( isset( $_POST[ $name ] ) {
$counter++
}
}
echo $counter;
回答by Niraj Chauhan
Using jQuery you can achieve it:
使用 jQuery 你可以实现它:
$("input:checkbox:checked").length
$("input:checkbox:checked").length
This will return the number of checboxes checked.
这将返回选中的复选框数。
And in php you'll need to pass it as an array.
在 php 中,您需要将其作为数组传递。
echo count($_POST['checkbox']);
echo count($_POST['checkbox']);
回答by Praind
$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++;
}
回答by nbar
All boxes that are checked will be in the Request when u click submit. In your case, if checkbox1 is checked u get: "checkbox1=box"
当您单击提交时,所有选中的框都将在请求中。在你的情况下,如果 checkbox1 被选中,你会得到:“checkbox1=box”
If you use GETas the method it will look like: http://yoururl.com/yourcode.php?checkbox1=boxand u can access it with $_GET['checkbox1']
如果您使用GET作为方法,它将如下所示:http: //yoururl.com/yourcode.php?checkbox1 = box并且您可以使用 $_GET['checkbox1'] 访问它
If you use POSTas the method you can access it with $_POST['checkbox1']
如果您使用POST作为方法,您可以使用 $_POST['checkbox1'] 访问它
You can also check if the box is checked (and in the Request data) with isset($_POST['checkbox1'])
您还可以使用 isset($_POST['checkbox1']) 检查该框是否被选中(以及在请求数据中)