Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Mar 2009
    Posts
    101
    Thanks
    14
    Thanked 1 Time in 1 Post

    Question Sending a $_POST for an unchecked checkbox?

    I'm setting up a for-each in an included file for all our Web pages, which converts all $_POST's into $_SESSION variables, so that if someone hits BACK while submitting a form, it's sure to repopulate the fields with what they typed.
    (Code samples at the bottom.)

    It works great, except if the submitter checks a checkbox, then goes back and decides to uncheck it, the $_SESSION remembers the checked value because an unchecked checkbox doesn't send a $_POST value.

    Maybe I just can't think outside the box enough (heh), but I can't think of a way to send an empty value or unset the value and still keep the $_POST-to-$_SESSION process uncluttered with if's for specific checkbox verification.

    Any ideas what I might try?

    Here's a sample of a form:

    [HTML]
    <tr>
    <?php unset($chk_flag);
    if ($_SESSION['chk_contactback'] == "1") { $chk_flag = "checked"; }
    ?>
    <td colspan="2"><input type="checkbox" name="chk_contactback" id="chk_contactback" value="1" <?=$chk_flag?> />
    <label for="chk_contactback">check this box if you would like to be contacted back</label></td>
    </tr>

    [/HTML]

    And here's the ubiquitous PHP that gets loaded on the submission of any form:

    PHP Code:
    //  MAKE POSTs INTO SESSION VARS
    //  (to re-fill forms if the BACK button is used)
    if (!empty($_POST)) {
        foreach (
    $_POST as $k=>$v) {
            unset(
    $_SESSION[$k]);
            
    $_SESSION[$k] = $v;
        } 

    Thanks!

  • #2
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Something like the following example should work:

    Code:
    $_SESSION['checkbox'] = ((isset($_POST['checkbox']) && !empty($_POST['checkbox'])) ? $_POST['checkbox'] : false);
    The session var will still be set, but a quick:

    Code:
    if ($_SESSION['checkbox'])
    will say whether it contains a value or not when you reference it.
    Last edited by MattF; 11-23-2010 at 07:13 PM.

  • #3
    Regular Coder
    Join Date
    Mar 2009
    Posts
    101
    Thanks
    14
    Thanked 1 Time in 1 Post
    Quote Originally Posted by MattF View Post
    Something like the following example should work:

    Code:
    $_SESSION['checkbox'] = ((isset($_POST['checkbox']) && !empty($_POST['checkbox'])) ? $_POST['checkbox'] : false);
    The session var will still be set, but a quick:

    Code:
    if ($_SESSION['checkbox'])
    will say whether it contains a value or not when you reference it.
    Hmm, correct me if I'm wrong, but if I'm reading that right, that example would require me to place script that references that specific checkbox ID in the PHP that converts $_POST to $_SESSION values?

    If so, that's exactly what I'm trying to avoid. That PHP is found in a separate file mentioned in an include() on every page -- so that EVERY form regardless of number and ID of the fields can benefit from being SESSION'ed.

    ...if I'm understanding your example right.
    Thanks for the reply!

  • #4
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    Store all of your form inputs in an array within the $_SESSION variable ( $_SESSION['form_data'] ). Then, check each and every input element in your $_SESSION['form_data'] array against your $_POST variables submitted in a loop. It won't matter how many or how few the items may be. If they are empty in the $_POST array then destroy them in the $_SESSION array in the manner MattF suggested.

    If your form is submitted across several pages, simply subdivide the $_SESSION['form_data'] array into arrays for each step ( $_SESSION['form_data']['step_1'] ). It seems to me that that should do the trick... but I'm pretty exhausted today so I may not be making complete sense.
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •