PHP在提交表单后保持复选框处于选中状态

PHP在提交表单后保持复选框处于选中状态,php,html,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"> <hea

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

<!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>

无标题文件

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

<input type="checkbox" name="txtCheck" value="1" <?php if (!empty($_POST['txtCheck'])): ?> checked="checked"<?php endif; ?> />
/>
但是,您可以保持
属性不变。

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

$checked = "";
if ($_POST['txtCheck']) {
  $checked = "checked";
  // May need to be "checked='checked'" for xhtml
}
<input type="checkbox" name="txtCheck" <?php echo $checked;?>  /><br />
$checked=”“;
如果($_POST['txtCheck'])){
$checked=“checked”;
//对于xhtml,可能需要“checked='checked'”
}
改变


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

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

如果复选框在数组中怎么办
name=“txtCheck[]”
谢谢,如果我不想在数组中选择它们,会发生什么。仅选定的一个
<input type="checkbox" name="txtCheck" value="your value" <?php if(isset($_POST['txtCheck'])) echo "checked='checked'"; ?>  /><br />
<input type="checkbox" value="1" name="nofollow" <?=isset($_POST['nofollow'])?"checked":''; ?> >