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 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了