php 从多个复选框中获取 POST 数据?

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

Get POST data from multiple checkboxes?

phppost

提问by Liam

Im trying to create a form using PHP and I cant seem to find a tutorial on what I need so thought Id ask on here.

我正在尝试使用 PHP 创建一个表单,但我似乎无法找到有关我需要什么的教程,所以我想在这里问一下。

I have a multiple checkbox option on my page...

我的页面上有多个复选框选项...

 <li>
    <label>What service are you enquiring about?</label>
    <input type="checkbox" value="Static guarding" name="service">Static guarding<br>
    <input type="checkbox" value="Mobile Patrols" name="service">Mobile Patrols<br>
    <input type="checkbox" value="Alarm response escorting" name="service">Alarm response escorting<br>
    <input type="checkbox" value="Alarm response/ Keyholding" name="service">Alarm response/ Keyholding<br>
    <input type="checkbox" value="Other" name="service">Other<input type="hidden" value="Other" name="service"></span>
  </li>

I'm not sure however how to collect all checkbox values using POST method?

但是,我不确定如何使用 POST 方法收集所有复选框值?

if i use

如果我使用

$service = $_POST['service'];

I only get 'other' returned

我只得到“其他”的回报

回答by Lekensteyn

Name the fields like service[]instead of service, then you'll be able to access it as array. After that, you can apply regular functions to arrays:

将字段命名为service[]而不是service,然后您就可以将其作为数组访问。之后,您可以将常规函数应用于数组:

  • Check if a certain value was selected:

     if (in_array("Other", $_POST['service'])) { /* Other was selected */}
    
  • Get a single newline-separated string with all selected options:

     echo implode("\n", $_POST['service']);
    
  • Loop through all selected checkboxes:

     foreach ($_POST['service'] as $service) {
         echo "You selected: $service <br>";
     }
    
  • 检查是否选择了某个值:

     if (in_array("Other", $_POST['service'])) { /* Other was selected */}
    
  • 获取包含所有选定选项的单个换行符分隔的字符串:

     echo implode("\n", $_POST['service']);
    
  • 循环遍历所有选定的复选框:

     foreach ($_POST['service'] as $service) {
         echo "You selected: $service <br>";
     }
    

回答by Watermark Studios

Currently it's just catching your last hidden input. Why do you have that hidden input there at all? If you want to gather information if the "Other" box is checked, then you have to hide the

目前它只是捕捉你最后一个隐藏的输入。为什么你在那里有那个隐藏的输入?如果您想在选中“其他”框的情况下收集信息,则必须隐藏

<input type="text" name="other" style="diplay:none;"/> 

and you can show it with javascript when the "Other" box is checked. Something like that.

当检查“其他”框时,您可以使用JavaScript显示它。类似的东西。

Just make the name attribute service[]

只需制作名称属性服务[]

<li>
<label>What service are you enquiring about?</label>
<input type="checkbox" value="Static guarding" name="service[]">Static guarding<br>
<input type="checkbox" value="Mobile Patrols" name="service[]">Mobile Patrols<br>
<input type="checkbox" value="Alarm response escorting" name="service[]">Alarm response escorting<br>
<input type="checkbox" value="Alarm response/ Keyholding" name="service[]">Alarm response/ Keyholding<br>
<input type="checkbox" value="Other" name="service[]">Other</span>
</li>

Then in your PHP you can access it like so

然后在你的 PHP 中你可以像这样访问它

$service = $_POST['service'];
echo $service[0]; // Output will be the value of the first selected checkbox
echo $service[1]; // Output will be the value of the second selected checkbox
print_r($service); //Output will be an array of values of the selected checkboxes

etc...

等等...

回答by Marc B

<input type="checkbox" value="Other" name="service">Other<input type="hidden" value="Other" name="service"></span>

You've got a hidden input field with the same name as the checkbox. "later" fields with the same name as an earlier one will overwrite the previous field's values. This means that your form, as posted above, will ALWAYS submit service=Other.

您有一个与复选框同名的隐藏输入字段。与较早字段同名的“稍后”字段将覆盖前一字段的值。这意味着您的表单,如上所述,将始终提交service=Other

Given the phrasing of your question in the html, it sounds more like you'd want a radio button, which allows only ONE of a group of same-name fields to be selected. Checkboxes are an "AND" situation, radio buttons correspond to "OR"

鉴于您的问题在 html 中的措辞,听起来更像是您想要一个单选按钮,它只允许选择一组同名字段中的一个。复选框是“AND”的情况,单选按钮对应“OR”