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 . '>';
isset() instead of assuming the key exists. Related: JavaScript checkbox handling and HTML forms.
$t1 = isset($_POST['t1']);
$t2 = isset($_POST['t2']);
$t1v = $t1 ? 'checked' : '';
$t2v = $t2 ? 'checked' : '';
echo "<form method='post' action=''>
<input type='checkbox' name='t1' value='yes' $t1v> First
<input type='checkbox' name='t2' value='yes' $t2v> Second
<input type='submit' value='Submit'>
</form>";
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! | |