For a sticky form, derive the checked attribute from submitted data instead of assuming the field will always be present.
$agreed = isset($_POST['agreed']);
$checked = $agreed ? ' checked' : '';
echo '<input type="checkbox" name="agreed" value="yes"' . $checked . '>';
This is the recommended starting pattern. The detailed examples below are retained for explanation, comparison and related variations.
$t1=$_GET['t1'];
$t2=$_GET['t2'];
if($t1=="yes"){$t1v="checked";}
else{$t1v=="";}
if($t2=="yes"){$t2v="checked";}
else{$t2v=="";}
echo "<form method=post action=pb-chk.php>
<input type=checkbox name=t1 value=yes $t1v> This is first checkbox<br>
<input type=checkbox name=t2 value=yes $t2v> This is second checkbox
<input type=submit value=Submit>";
To keep the second page simple we will keep only the redirection part. Here we have excluded the part which takes care if the form passes its validation and execute the actual script. $t1=$_POST['t1']; // receive form ( checkbox value) data using POST method
$t2=$_POST['t2'];
header ("Location: pb-check.php?t1=$t1&t2=$t2"); // redirect to First page or Form page
Difference between GET and POST method of Form submission
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.
| AARON | 04-01-2019 |
| I have been searching the whole net for this and here I am with the true answer to my problem. Thank you! | |