doutang3760 2012-03-09 03:25
浏览 50
已采纳

将复选框的值传递给数组是否已选中

I need to pass the value and a couple of other values depending on what checkboxes are checked. I found posted a couple of other times here to rely on html for this and pass a hidden value before the checkbox :

<input type="hidden" value="0"  name="B_1" />
<input type="checkbox" value="1"  name="B_1" />
<input type="hidden" value="0"  name="B_2" />
<input type="checkbox" value="1"  name="B_2" />
<input type="hidden" value="0"  name="B_3" />
<input type="checkbox" value="1"  name="B_3" />

Then in the php I am making different associations based on these checkboxes :

$b = "Buyers";
$bv1 = "Not a web item";
$bv2 = "Need sample";
$bv3 = "Sample not available";

if ($_POST['B_1']) { $b1 = array( $b , $bv1 , $_POST['B_1'] ); } 
if ($_POST['B_2']) { $b2 = array( $b , $bv2 , $_POST['B_2'] ); } 
if ($_POST['B_3']) { $b3 = array( $b , $bv3 , $_POST['B_3'] ); } 

when I use print_r I am only seeing the arrays for those boxes I checked :

Array ( [0] => Buyers [1] => Not a web item [2] => 1 )

I expect to see all of the arrays returned regardless of state of checkbox.

Array ( [0] => Buyers [1] => Not a web item [2] => 0 ) 
Array ( [0] => Buyers [1] => Need sample [2] => 1 ) 
Array ( [0] => Buyers [1] => Sample not available [2] => 0 )
  • 写回答

1条回答 默认 最新

  • douliedai4838 2012-03-09 03:33
    关注

    I don't see the use of your hidden fields here at all (also, they shouldn't have the same name as other elements, unless the field is an array). If you're not using them, I propose that you change your HTML to:

    <input type="checkbox" value="B_1"  name="B_1" />
    <input type="checkbox" value="B_2"  name="B_2" />
    <input type="checkbox" value="B_3"  name="B_3" />
    

    You can store whatever value you need in the checkbox element value attribute.

    If you need all of the arrays, regardless of whether or not the boxes are checked, the if should be removed:

    $b1 = array( $b , $bv1 , $_POST['B_1'] );
    $b2 = array( $b , $bv2 , $_POST['B_2'] );
    $b3 = array( $b , $bv3 , $_POST['B_3'] );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于大棚监测的pcb板设计
  • ¥20 sim800c模块 at指令及平台
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计