I’ve got a form with a checkbox. When a user first visits the page, I want the checkbox to be ‘checked’. However, if they uncheck the box and then submit the page, I want it to remain unchecked (and to remain checked if they submit the page with it checked).
To determine when it has been checked and the form submitted, I’m currently doing:
<input type='checkbox' class='seeAll' name='seeAll' value='true' <?php if ($_POST['seeAll'] == 'true') echo checked; ?>>
This works great for leaving the box checked when needed, however, how would I go about ensuring it stays unchecked if they submit it that way, while also being checked if they revisit the page (such as by re-entering the URL)?
Thanks!
I don’t know why it took me so long to come up with this answer, but after struggling with this, I realized I could just check the value of the checkbox via the
$_POST, as I was doing before and could check if the user arrived at the page by some means other than the submit button by doing this:If the user submitted the form, than
isset($_POST['submit'])will be true and so if that’s the case and$_POST['seeAll']is empty, them obviously the user submitted an unchecked box. Ifisset($_POST['submit'])is false, then the user arrived on the page without submitting the form and I should check the checkbox as the ‘default’.So then my whole
<input>tag looks like this:Works just as I need!