POST 数组未显示未选中的复选框

POST arrays not showing unchecked Checkboxes(POST 数组未显示未选中的复选框)
本文介绍了POST 数组未显示未选中的复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

无法让我的 POST 数组显示表单中的所有复选框值.

Having trouble getting my POST arrays to show all checkbox values from my form.

我的表单设置如下:

<form name='foo' method='post' action=''>
    <table>
       <tr>
          <td class='bla'>Checkbox: <input type='checkbox' name='cBox[]'/></td>
      </tr>
       <tr>
          <td class='bla'>Checkbox: <input type='checkbox' name='cBox[]'/></td>
      </tr>
       <tr>
          <td class='bla'>Checkbox: <input type='checkbox' name='cBox[]'/></td>
      </tr>
   </table>
</form>

我在底部有一个按钮绑定到一个 jquery 函数,它向表单添加了另外 5 个空行(因此输入名称 cBox[] 的数组).

I have a button at the bottom bound to a jquery function that adds 5 more empty rows to the form (hence the arrays for the input name cBox[]).

现在,问题来了.假设第一个复选框未选中,最后两个复选框被选中.当我输出值时(使用 PHP print_r 进行调试),我会得到:

Now, the problem. Lets say the first checkbox is unchecked, and the last 2 are checked. When I output the values (using PHP print_r for debugging), I will get:

Array ( [0] => on [1] => on)

出于某种原因,数组不包含任何未选中复选框的值.

For some reason, the array does not contain any value for unchecked checkboxes.

我已经看到一些解决方案,其中每个复选框都传递一个隐藏变量,但是这个解决方案可以在我的情况下实现吗(使用数组)?

I have seen some solutions where a hidden variable is passed with each checkbox, but can this solution be implemented in my situation (using arrays)?

推荐答案

这种行为并不奇怪,因为浏览器不会为未选中的复选框提交任何值.

That behavior is not surprising, as the browser doesn't submit any value for checkboxes that are unchecked.

如果您需要将确切数量的元素作为数组提交,为什么不做与有某种相关联的 id 时所做的相同的事情每个复选框?只需在 元素名称中包含 PHP 数组键名:

If you are in a situation where you need to submit an exact number of elements as an array, why don't you do the same thing you do when there's an id of some sort associated with each checkbox? Just include the PHP array key name as part of the <input> element's name:

  <tr>
                                                       <!-- NOTE [0] --->
      <td class='bla'>Checkbox: <input type='checkbox' name='cBox[0]'/></td>
  </tr>
   <tr>
      <td class='bla'>Checkbox: <input type='checkbox' name='cBox[1]'/></td>
  </tr>
   <tr>
      <td class='bla'>Checkbox: <input type='checkbox' name='cBox[2]'/></td>
  </tr>

这仍然给您留下未选中的框仍然不会出现在数组中的问题.这可能是也可能不是问题.一方面,你可能真的不在乎:

That still leaves you with the problem that unchecked boxes will still not be present in the array. That may or may not be a problem. For one, you may really not care:

foreach($incoming as $key => $value) {
    // if the first $key is 1, do you care that you will never see 0?
}

即使您很在意,也可以轻松纠正问题.这里有两种直接的方法.一,只做隐藏的输入元素技巧:

Even if you do care, you can easily correct the problem. Two straightforward approaches here. One, just do the hidden input element trick:

  <tr>
      <td class='bla'>
        <input type="hidden" name="cBox[0]" value="" />
        Checkbox: <input type='checkbox' name='cBox[0]'/>
      </td>
  </tr>
   <tr>
      <td class='bla'>
        <input type="hidden" name="cBox[1]" value="" />
        Checkbox: <input type='checkbox' name='cBox[1]'/>
      </td>
  </tr>

还有两个,我觉得更可取,用 PHP 代替:

And two, which I find preferable, fill in the blanks from PHP instead:

// assume this is what comes in:
$input = array(
    '1' => 'foo',
    '3' => 'bar',
);

// set defaults: array with keys 0-4 all set to empty string
$defaults = array_fill(0, 5, '');

$input = $input + $defaults;
print_r($input);

// If you also want order, sort:

ksort($input);
print_r($input);

查看实际操作.

这篇关于POST 数组未显示未选中的复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)