2012-09-22 94 views
7

大家好,我有一個聯繫表和captcha在那裏。我想保持提交表單後檢查檢查。我發佈了文本框的值,它顯示正確,但複選框不起作用。這是我的代碼。提交表格後PHP保持複選框檢查

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

如何在提交表單後保留複選框。

回答

17

變化

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

這將讓選中複選框..

+0

是的,它是工作正常..感謝很多... – Rakesh

+2

如果複選框是在一個數組內? 'name =「txtCheck []」' – Beaniie

+1

'<?php if(in_array(「your_value」,$ _POST ['txtCheck']))echo「checked ='checked'」; ?>' – GeoGyro

1

如果提交的值不爲空,那麼checked="checked"屬性添加到複選框:

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

但是,您可以離開value屬性不變。

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

試試這個:

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