【问题标题】:Reading dynamic checkbox array in php/html在 php/html 中读取动态复选框数组
【发布时间】:2016-08-14 03:24:49
【问题描述】:

我有多个动态创建的表单域。

<label><input type="text" name="txtAmt[]">Amount</label>
<label><input type="checkbox" name="cbMarkPaid[]">Mark as Paid</label>

我使用 post 方法将 for 提交到 php 文件。我知道复选框不会在您未选中时将数据发送到服务器。但我想知道未选中的复选框的索引.

例如如果我有 4 个复选框 cb1,cb2,cb3,cb4 并且如果 cb3,cb4 被选中。然后 php 收到一个包含两个项目的数组:

array(2) { [0]=> string(2) "on" [1]=> string(2) "on" }

但我希望数组为:

array(2) { [0]=> string(2) "off" [1]=> string(2) "off" [2]=> string(2) "on" [3]=> string (2) “开”}。

请不要isset($_POST['cbMarkPaid'][]) 也给出相同的输出 谢谢

【问题讨论】:

  • 共享完整标记...您只提供了 2 个字段...

标签: javascript php arrays html checkbox


【解决方案1】:

您不能通过 POST 上的复选框来执行此操作。根据定义,unchecked checkboxes are never successful

成功的控件对于提交是“有效的”。
所有“打开”复选框都可能成功。

您可以做的最好的事情是索引您的数组并寻找差距。例如

<label><input type="text" name="txtAmt[0]">Amount</label>
<label><input type="checkbox" name="cbMarkPaid[0]">Mark as Paid</label>
<label><input type="text" name="txtAmt[1]">Amount</label>
<label><input type="checkbox" name="cbMarkPaid[1]">Mark as Paid</label>

假设您未选中第一个,选中第二个。然后你的数组看起来像

array(1) { [1]=> string(2) "on" }

所以你知道[0] 已关闭,因为它没有提交

【讨论】:

    猜你喜欢
    • 2013-09-28
    • 1970-01-01
    • 1970-01-01
    • 2011-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-12
    • 1970-01-01
    相关资源
    最近更新 更多