使用 php 获取多个复选框名称/id

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4631224/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 13:46:03  来源:igfitidea点击:

getting multiple checkboxes names/id's with php

phpformscheckbox

提问by John

How can i get the names or id's of the multiple selected checkboxes on submit, using the PHP? Following is example form. Thanks.

如何使用 PHP 在提交时获取多个选定复选框的名称或 ID?以下是示例表格。谢谢。

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
   <input type="checkbox" name="orange" id="orange">
   <input type="checkbox" name="apple" id="apple">
   <input type="checkbox" name="sky" id="sky">
   <input type="checkbox" name="sea" id="sea">
   <br>
   <br>
   <input type="submit" name="Submit" value="Submit">
</form>

回答by David Powers

Checkbox values are submitted from a form only if the checkbox is selected. What's more, it's the name attribute that counts, not the ID.

Checkbox values are submitted from a form only if the checkbox is selected. 更重要的是,重要的是名称属性,而不是 ID。

There are several ways of handling checkboxes in PHP:

PHP 中有几种处理复选框的方法:

  1. Give all checkboxes the same name followed by a pair of square brackets, so the entire set is treated as an array. In this case, give each checkbox a value.
  2. Give each checkbox a different name and a value.
  3. Give each checkbox a different name, but no value.
  1. 为所有复选框指定相同的名称,后跟一对方括号,以便将整个集合视为一个数组。在这种情况下,给每个复选框一个值。
  2. 为每个复选框指定不同的名称和值。
  3. 为每个复选框指定不同的名称,但没有值。

In each case, you need to check for the existence of the checkbox name in the $_POST array.

在每种情况下,您都需要检查 $_POST 数组中是否存在复选框名称。

For example:

例如:

<input type="checkbox" name="color[]" id="orange" value="orange">
<input type="checkbox" name="color[]" id="apple" value="apple">

To get the values for these checkboxes:

要获取这些复选框的值:

if (isset($_POST['color'])) {
    $colors = $_POST['color'];
    // $colors is an array of selected values
}

However, if each checkbox has a different name and an explicit value like this:

但是,如果每个复选框都有不同的名称和显式值,如下所示:

<input type="checkbox" name="orange" id="orange" value="orange">
<input type="checkbox" name="apple" id="apple" value="apple">

You still need to use isset():

您仍然需要使用isset():

if (isset($_POST['orange'])) {
    // orange has been set and its value is "orange"
}

If you don't set a value, the default value is "on", but it won't be in the $_POST array unless it has been selected, so you still need to use isset().

如果不设置值,默认值为“on”,但除非已被选中,否则它不会出现在 $_POST 数组中,因此您仍然需要使用 isset()。

回答by Abdullah Jibaly

You need to give the inputs the same name:

您需要为输入提供相同的名称:

<input type="checkbox" name="selection[]" value="orange">
<input type="checkbox" name="selection[]" value="apple">
<input type="checkbox" name="selection[]" value="sky">
<input type="checkbox" name="selection[]" value="sea">

Then iterate over the $_POST['selection'] array in PHP.

然后在 PHP 中迭代 $_POST['selection'] 数组。

回答by webbiedave

You won't get the ids but the names will be associative indexes in the $_POSTarray (and $_REQUEST). NOTE: They will only be available in the array if they were checked by the client.

您不会获得 id,但名称将是$_POST数组(和$_REQUEST)中的关联索引。注意:只有在客户端检查它们时,它们才会在阵列中可用。

if ($_POST['oragne'] == 'on')

if ($_POST['oragne'] == 'on')

回答by Mahdi.Montgomery

You can set them up to post to PHP as arrays, if you build them similar to below:

您可以将它们设置为作为数组发布到 PHP,如果您构建它们类似于以下内容:

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
   <input type="checkbox" name="box_group_1[oragne]" id="oragne">
   <input type="checkbox" name="box_group_1[apple]" id="apple">
   <input type="checkbox" name="box_group_1[sky]" id="sky">
   <input type="checkbox" name="box_group_1[sea]" id="sea">
   <br>
   <br>
   <input type="submit" name="Submit" value="Submit">
</form>
<?php
print_r($_POST['box_group_1']);
?>