Im aware that this problem of mine could be resolved by using javasrcipt but in this case its not an option, PHP only.
There is a search form like this:
<form method="post" action="">
<input type="checkbox" name="username">
<input type="checkbox" name="email">
<input type="checkbox" name="name">
//irrelevant text inputs
<input type="submit" value="submit">
</form>
Now if user decide to check username and name - script must keep it checked after submit, we could accomplish this easly in following way:
<input type="checkbox" name="username" <?php if(isset($_POST['username'])) { echo 'checked'; } ?>>
This may not seem desirable in this simple example but in real code there is around 30 different checkboxes so we need to specify default selection for most conducted searches, also easly doable this way:
<input type="checkbox" name="username" <?php if(isset($_POST['username'])) { echo 'checked'; } else { echo 'checked'; } ?>>
At this point following problem appears:
if something has been checked by default and user unchecked it and submited form it will again check itselfe due to that } else { echo 'checked'; }
conditio
unfortunetely submitting unchecked checkbox doesnt create empty key in $_POST
array therefore i dont see how to approach this issue.
What im trying to accomplish is to:
- set some of checkboxes to default check state
- keep pre-submit checkboxes state to "after submit" (so they wont go back to opposite state)
lets make some visual representation of my problem:
-
user enters a page:
☑ username ☑ email ☒ name
-
user selects desirable checkboxes:
☒ username ☑ email ☑ name
-
user submits form and after submit it still looks the same way:
☒ username ☑ email ☑ name