php - Pass value of checkbox to array whether it is checked or not -
i need pass value , couple of other values depending on checkboxes checked. found posted couple of other times here rely on html , pass hidden value before 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 php making different associations based on these checkboxes :
$b = "buyers"; $bv1 = "not 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 use print_r
seeing arrays boxes checked :
array ( [0] => buyers [1] => not web item [2] => 1 )
i expect see of arrays returned regardless of state of checkbox.
array ( [0] => buyers [1] => not web item [2] => 0 ) array ( [0] => buyers [1] => need sample [2] => 1 ) array ( [0] => buyers [1] => sample not available [2] => 0 )
i don't see use of hidden fields here @ (also, shouldn't have same name other elements, unless field array). if you're not using them, propose change 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 need in checkbox element value
attribute.
if need of arrays, regardless of whether or not boxes checked, if
should removed:
$b1 = array( $b , $bv1 , $_post['b_1'] ); $b2 = array( $b , $bv2 , $_post['b_2'] ); $b3 = array( $b , $bv3 , $_post['b_3'] );
Comments
Post a Comment