WebRef.eu  - Internet Marketing and Online Business Resources  

Home / Site Map | Blog | TwitterTwitter | Contact

Back to PHP Scripts List

Remember Checkbox Status on Postback in PHP

A popular technique for form processing in PHP is to get the form to postback to itself, using the following form action:

<form name="form1" method="post" action="<? echo $_SERVER['PHP_SELF']; ?>">

If there are errors in the form, then it will get redisplayed to the user. You need to make sure that the data the user input is redisplayed correctly. This includes any checkboxes - if the user checked a particular checkbox, you want that to be redisplayed as checked, i.e. the checkbox status is "remembered" when the form is redisplayed.

Below we give the code for a PHP script that demonstrates how to remember the checkbox status. Copy and paste the script and save the file with a .php extension to test it on your server. You can also view the script in action.

<!-- SCRIPT BEGINS -->

<html>
<head>
<title>PHP Script - Remember checkbox status on postback in PHP</title>
<meta name="description" content="An example PHP script which shows how to remember the checked status of a checkbox on form postback.">
<meta name="keywords" content="remember checkbox status.">

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<style type="text/css">
<!--
.txterror {
color: #FF0000;
}
-->
</style>

</head>

<body>

<?php
//If processform flag is set, processing can occur. If not set, form is being loaded for the first time
if ($_POST['processform'] == 1) {

//Process Form
//retrieve form input

$Username=$_POST['txtUsername'];

if(isset($_POST['RememberMe'])) {
$RememberMeCheckboxStatus = "checked";
}

//Validate fields

// create empty error variable
// always concatenate error variable so you don't wipe out other error messages
$ErrorMsg = "";

//Username can't be left blank
if (!$Username) {
$ErrorMsg = $ErrorMsg . "Please give your Username.<br>";
}

echo "<p class='txterror'>" . $ErrorMsg . "</p>";

//If no errors then form input successful and can move to next step e.g. write to database
//PrepareForForm function used again so single and double quotes displayed correctly with magic quotes affecting them
If ($ErrorMsg == "") {
echo "<p>Form processing successful.</p>";
}

//Close If processform flag is set
}

//If condition for when to display form
//when processform flag is 1 and ErrorMsg is empty we don't want to display the form
//otherwise we display the form for error correction
If (!($_POST['processform'] == 1 && $ErrorMsg == "")) {
?>
Leave the Username blank in the form below, and check the Remember Me checkbox, then click Submit. You will see that when the form is posted back, the form remembers that the checkbox was checked. Take a look at the source code of the page in your browser and you will see <b>checked</b> appearing in the tag for the Remember Me checkbox.
<br /><br />
Next, leaving the Username blank again, uncheck Remember Me and Submit again. Look at the page source code again and you'll see that the <b>checked</b> has disappeared from the tag for the Remember Me checkbox.
<br>
<br>
<form name="form1" method="post" action="<? echo $_SERVER['PHP_SELF']; ?>">

Username (leave this blank to trigger an error):<br />
<input type="text" name="txtUsername" size="20" maxlength="20" value="<?print $Username; ?>" />
<br>
<br>

<input type="checkbox" name="RememberMe" <?PHP print $RememberMeCheckboxStatus; ?>/>
Remember Me next time<br /><br />

<!-- processform flag so you can tell when form is posted back -->
<input type="hidden" name="processform" value="1">

<input type="submit" name="Submit" value="Submit">

</form>

<?php
//End If condition for when to display form
}
?>

<br>
<a href="index.php">Back to Home Page</a>
</body>
</html>

<!-- SCRIPT ENDS -->

 




Low Prices UK Shopping

Compare Prices
at LowPrices.co.uk


Home / Site Map | TwitterTwitter | Contact

All Content ©2018 WebRef.eu