提交表单后,PHP保持复选框选中状态

时间:2012-09-22 06:40:05

标签: php html

大家好,我有一个联系表格,验证码就在那里。我希望在提交表格后检查支票。我发布了文本框值,它显示正确但复选框不起作用。这是我的代码。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org /TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form action = "" name="frmSubmit" method="post">
<input type="checkbox" name="txtCheck" value="<?php echo $_POST['txtCheck'];?>"  /><br />
<label>Name</label><br />
<input type="text" name="txtName" id="NameTextBox" value="<?php echo $_POST['txtName']; ?>" />
<br />
<label>E Mail</label><br />
 <input type="text" name="txtEmail" id="EmailTextBox" value="<?php  echo $_POST['txtEmail'];?>" />
 <input name="BtnSubmit" type="submit" onclick="MM_validateForm('NameTextBox','','R','EmailTextBox','','R');return document.MM_returnValue" value="Send" />
</form>
</body>
</html>

提交表单后如何保留复选框。?

6 个答案:

答案 0 :(得分:21)

变化

<input type="checkbox" name="txtCheck" value="<?php echo $_POST['txtCheck'];?>"  /><br />

<input type="checkbox" name="txtCheck" value="your value" <?php if(isset($_POST['txtCheck'])) echo "checked='checked'"; ?>  /><br />

这将保持复选框..

答案 1 :(得分:1)

如果提交的值不为空,则将checked="checked"属性添加到复选框:

<input type="checkbox" name="txtCheck" value="1" <?php if (!empty($_POST['txtCheck'])): ?> checked="checked"<?php endif; ?> />

但您可以保持value属性不变。

答案 2 :(得分:0)

<input type="checkbox" name="txtCheck" <?php if($_POST['txtCheck']>0){ ?>checked="checked" <? }?> />

答案 3 :(得分:0)

试试这个:

$checked = "";
if ($_POST['txtCheck']) {
  $checked = "checked";
  // May need to be "checked='checked'" for xhtml
}
<input type="checkbox" name="txtCheck" <?php echo $checked;?>  /><br />

答案 4 :(得分:0)

通过在复选框标签字段中应用三元条件,我们可以设置是否在表单提交时选中此条件

<input type="checkbox" value="1" name="nofollow" <?=isset($_POST['nofollow'])?"checked":''; ?> >

答案 5 :(得分:0)

<form method='POST' action''>
    <input class = chkbox type="checkbox" name="chk_box" checked>
</form>