提交表单后如何保存复选框状态

时间:2014-10-17 06:10:55

标签: php forms

我有一个PHP代码来显示表数据,其中包含一列复选框,用于单击以将测试用例标记为已阻止。我试图在提交后保存复选框的状态,但我无法这样做。 请帮忙!

echo "<form id=\"checkbox\" class=\"check2\" method = \"post\" action=\"\">";
$checked = "";
if(isset($_POST['Blocked[]'])) {
$checked = 'checked="checked"';
}
echo "<td $Blocked><input type =\"checkbox\" name=\"Blocked[]\" value=\"checkblock\"         onclick=\"showMsg('div1')\" $checked/></td>";             
echo "<input type=\"submit\" value=\"Submit\" class=\"button\" name=\"edit_tc\" onclick=\"myFunction(form)\" style=\"position:fixed; height:25px ; width:150px; bottom:25px; right:200px;\"/>";
echo "</form>";

1 个答案:

答案 0 :(得分:0)

首先,请不要像上面那样回应静态html,因为它会使代码阅读变得困难。其次,您正在错误地使用isset()函数。第三,复选框的输入字段名称是数组类型。你真的需要这个作为阵列吗?

请使用以下内容:

<?php
$checked = '';
if(isset($_POST['Blocked'])) {
    $checked = 'checked="checked"';
}
?>
<form id="checkbox" class="check2" method = "post" action="">
<input type ="checkbox" name="Blocked" value="checkblock" onclick="showMsg('div1')" <?php echo $checked;?>/>
<input type="submit" value="Submit" class="button" name="edit_tc" onclick="myFunction(form)" style="position:fixed; height:25px ; width:150px; bottom:25px; right:200px;"/>
</form>