Multiple Checkbox not checking after submitting [closed]

↘锁芯ラ 提交于 2019-12-25 18:38:36

问题


$color="";<br>
$sel4="";<br>
$suc="";<br>
if(isset($_POST['save']))   <br>
{<br>
$e="0";<br>
if(isset($_POST['color']))<br>
{<br>
    $color=$_POST['color'];<br>
}<br>
else<br>
{<br>
    $sel4="<--Check Any Color";<br>
    $e="1";<br>
}<br>
if($e==0)<br>
{<br>
$suc="Success";<br>
}<br>
}<br>
?><br><br><br>
~~body~~<br>
~~form method="POST"~~<br>
~~table~~<br>
~~tr~~<br>
~~td~~<br>
color<br>
~~/td~~<br>
~~td~~<br>
~~input type="checkbox" name="color" value="blue" ~~? if($color=='blue'){ echo "checked";  }?>  /><br>Blue<br>
~~input type="checkbox" name="color" value="red" ~~? if($color=='red'){ echo "checked"; }?>  /><br>Red<br>
~~input type="checkbox" name="color" value="green" ~~? if($color=='green'){ echo "checked";  }?>  />Green<br>
~~input type="checkbox" name="color" value="yellow" ~~? if($color=='yellow'){ echo "checked";   }?>  />Yellow<br>
~~/td~~<br>
~~td~~?=$sel4;?~~<br>
~~/tr~~<br>
~~tr~~<br>
~~td colspan="3"~~<br>
~~input type="submit" name="save"~~<br>
~~/td~~<br>
~~?=$suc?~~<br>
~~/tr~~<br>
~~/table~~<br>
~~/form~~<br>
~~/body~~<br>

<br><br><br>

NOTE: html tag<> not used here. So i used "~~" instead of "<>".

Subject: This program working perfectly.. But when i submitting with MORE THAN ONE check box., the checked items is not in CHECKED.
Only one item is checked., after submitting the form.<br><br>

For Example:
When i check 'Blue' and 'Red' color., it Checked only 'Red' color after Submitting. So i need the solution for this error. Help Me!.


回答1:


you should declare your checkbox as arrays

<input type='checkbox' name='color_list[]' value="red">
<input type='checkbox' name='color_list[]' value="blue">
<input type='checkbox' name='color_list[]' value="green">

so that after you get it $_POST['color_list'] you'll have the array of colors selected. you could do something like this:

$colors_selected = $_POST['color_list'];

then just use some function like implode(). I hope this helps.




回答2:


here is correct code to checked your checkbox if you $color content green

<input type="checkbox" name="color" value="green" <? if($color=='green'){ echo "checked"; }?> />

i think as you have used ~~ before php tag you have closed input tag



来源:https://stackoverflow.com/questions/16293024/multiple-checkbox-not-checking-after-submitting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!