0

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

3
  • 3
    Is this what you're trying to do? Commented Jul 21, 2014 at 13:16
  • this is exactly what i needed, simple and effective solution Commented Jul 21, 2014 at 14:09
  • @billyonecan Make it an answer then, so this question can be closed. OP seems to agree. Commented Jul 21, 2014 at 14:35

2 Answers 2

3

Give your input[type="submit"] a name, you can use that to determine whether or not the form was submitted:

<input type="submit" name="submitted" value="submit" />

Then you can use that as part of the condition for checking the checkboxes:

<input type="checkbox" name="username"<?php echo !isset($_POST['submitted']) || isset($_POST['username']) ? ' checked' : ''; ?> ... />

Alternatively, you can check $_SERVER['HTTP_REQUEST_METHOD'] to determine whether or not the form was posted.

<input ... <?php echo $_SERVER['REQUEST_METHOD'] != 'POST' || isset($_POST['username']) ? ' checked' : ''; ?> ... />

Here's an example

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for this answer, without getting into sessions or cookies this has been simply cracked:)
0

It would make way more sense if the default is as you mentioned, then as soon as the form is submitted it only takes values from the $_POST. For example,

if(empty($_POST)){ // if the form hasn't been submitted
    echo " checked='checked'"; // default setting for 'name';
}elseif(isset($_POST['name'])){ // otherwise it has been submitted, 
                                // so if this field is filled echo check
    echo "checked='checked'";
}else{
    // do nothing (you can delete this bit)
}

If not, you could always use JavaScript on click to change type='hidden' fields and check what they are set to using PHP at the other end.

2 Comments

$_POST will always be set
Sorry, empty($_POST) not !isset($_POST) as I had previously.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.