php 提交表单后PHP保持复选框选中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12541419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP keep checkbox checked after submitting form
提问by Rakesh
Hi all i have a contact form and captcha is there. i want keep the check is checked after submitting the form. I posted the textbox values and it showing correctly but checkbox is not working. here is my code.
大家好,我有一个联系表格和验证码。我想在提交表格后检查支票。我发布了文本框值并且它显示正确但复选框不起作用。这是我的代码。
<!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>
How to keep check box after submitting the form.?
提交表单后如何保留复选框。?
回答by Yogesh Suthar
change
改变
<input type="checkbox" name="txtCheck" value="<?php echo $_POST['txtCheck'];?>" /><br />
to
到
<input type="checkbox" name="txtCheck" value="your value" <?php if(isset($_POST['txtCheck'])) echo "checked='checked'"; ?> /><br />
this will keep checkbox checked..
这将保持复选框选中..
回答by smottt
If the submitted value is not empty, then add the checked="checked"attribute to the checkbox:
如果提交的值不为空,则checked="checked"在复选框中添加属性:
<input type="checkbox" name="txtCheck" value="1" <?php if (!empty($_POST['txtCheck'])): ?> checked="checked"<?php endif; ?> />
You can however leave the valueattribute intact.
但是,您可以value保持该属性不变。
回答by Sibu
<input type="checkbox" name="txtCheck" <?php if($_POST['txtCheck']>0){ ?>checked="checked" <? }?> />
回答by Jeremy J Starcher
Try this:
尝试这个:
$checked = "";
if ($_POST['txtCheck']) {
$checked = "checked";
// May need to be "checked='checked'" for xhtml
}
<input type="checkbox" name="txtCheck" <?php echo $checked;?> /><br />
回答by Mohsin Shoukat
By apply ternary condition in checkbox tag field we can set whether this is checked or not on form submission
通过在复选框标签字段中应用三元条件,我们可以设置在表单提交时是否选中此项
<input type="checkbox" value="1" name="nofollow" <?=isset($_POST['nofollow'])?"checked":''; ?> >
回答by Furki
<form method='POST' action''>
<input class = chkbox type="checkbox" name="chk_box" checked>
</form>

