Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP keep checkbox checked after submitting form

Tags:

html

php

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.?

like image 844
Rakesh Avatar asked Sep 22 '12 06:09

Rakesh


Video Answer


1 Answers

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

like image 129
Yogesh Suthar Avatar answered Sep 23 '22 19:09

Yogesh Suthar