PHP - 复选框组 [英] PHP - Checkbox group

查看:134
本文介绍了PHP - 复选框组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < input type =复选框我的复选框组以一种形式发布, value =truechecked name =chk0 []> 
< input type =checkboxvalue =falsename =chk0 []>
< input type =checkboxvalue =falsename =chk0 []>
< input type =checkboxvalue =truechecked name =chk0 []>

< input type =checkboxvalue =truechecked name =chk1 []>
< input type =checkboxvalue =falsename =chk1 []>
< input type =checkboxvalue =truechecked name =chk1 []>
< input type =checkboxvalue =falsename =chk1 []>

请注意,在第一组中,检查1和4。在第二组中,检查1和3。
当我发布这篇文章时,我会依次获取选中复选框的发布值:

  [chk0] = >数组(
[0] => true
[1] => true

[chk1] => Array(
[0] => true
[1] => true

我如何确保获得像这样的发布值:

  [chk0] => ; Array(
[0] => true
[3] => true

[chk1] => Array(
[0] => true
[2] => true

我想知道选中复选框的键,而不是按顺序显示我。



谢谢

解决方案

 < input type =checkboxvalue =truechecked name =chk0 [0]> 
< input type =checkboxvalue =falsename =chk0 [1]>
< input type =checkboxvalue =falsename =chk0 [2]>
< input type =checkboxvalue =truechecked name =chk0 [3]>

< input type =checkboxvalue =truechecked name =chk1 [0]>
< input type =checkboxvalue =falsename =chk1 [1]>
< input type =checkboxvalue =truechecked name =chk1 [2]>
< input type =checkboxvalue =falsename =chk1 [3]>

查看我的意思?

<输入名称=chk0 [1]...> 框与PHP中的数组元素一样工作。您可以指定将它们原样传输到PHP中的索引,如 $ _ POST ['chk0'] [1] 元素。


I have a checkbox group in one form that I need to be posted.

<input type="checkbox" value="true" checked name="chk0[]">
<input type="checkbox" value="false" name="chk0[]">
<input type="checkbox" value="false" name="chk0[]">
<input type="checkbox" value="true" checked name="chk0[]">

<input type="checkbox" value="true" checked name="chk1[]">
<input type="checkbox" value="false" name="chk1[]">
<input type="checkbox" value="true" checked name="chk1[]">
<input type="checkbox" value="false" name="chk1[]">

Note that in the first group, 1 and 4 are checked. In the second group, 1 and 3 are checked. When I post this, I get the posted values for the checked checkboxes sequentially as under:

[chk0] => Array ( 
          [0] => true 
          [1] => true 
        ) 
[chk1] => Array ( 
          [0] => true 
          [1] => true 
        ) 

How can I make sure I get the posted values like this:

[chk0] => Array ( 
          [0] => true 
          [3] => true 
        ) 
[chk1] => Array ( 
          [0] => true 
          [2] => true 
        ) 

I wanted to know the keys of the checked checkboxes instead of showing me sequentially.

Thanks

解决方案

<input type="checkbox" value="true" checked name="chk0[0]">
<input type="checkbox" value="false" name="chk0[1]">
<input type="checkbox" value="false" name="chk0[2]">
<input type="checkbox" value="true" checked name="chk0[3]">

<input type="checkbox" value="true" checked name="chk1[0]">
<input type="checkbox" value="false" name="chk1[1]">
<input type="checkbox" value="true" checked name="chk1[2]">
<input type="checkbox" value="false" name="chk1[3]">

See what I mean?
The <input name="chk0[1]" ...> boxes work just like array elements inside PHP. You can specify the indexes an they will be transfered as-is into PHP as $_POST['chk0'][1] elements.

这篇关于PHP - 复选框组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆