Unsetting session variables when a checkbox is set to unchecked

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Pete

    Unsetting session variables when a checkbox is set to unchecked

    I have some session variables which get set on page one of my site. I
    can set an unset these by passing through a parameter in some URL's.
    When the user goes to the second page some checkboxes are checked if
    their corresponding session variable is set and has a value of "true".

    The problem I have is that if the user unchecks a checkbox before
    finally moving to page three (which is perfectly okay for them to do if
    they wish) then I need to unset the session variable. It's not a
    problem if it goes from unchecked to checked as I can check for the
    value of the checkbox in page 3 and if it's set then I can set the
    corresponding session variable however as an unchecked checkbox is not
    sent through as part of the $_POST array then I obviously can't do a
    check for it. What I really need to do is alter the session variable as
    soon as they update the checkbox.

    Can anyone help? I can kind of think of a solution but it involves
    JavaScript (again) but that's a story for a different newsgroup...

    Thanks,

    Pete.

  • Pedro Graca

    #2
    Re: Unsetting session variables when a checkbox is set to unchecked

    Pete wrote:[color=blue]
    > The problem I have is that if the user unchecks a checkbox before
    > finally moving to page three (which is perfectly okay for them to do if
    > they wish) then I need to unset the session variable. It's not a
    > problem if it goes from unchecked to checked as I can check for the
    > value of the checkbox in page 3 and if it's set then I can set the
    > corresponding session variable however as an unchecked checkbox is not
    > sent through as part of the $_POST array then I obviously can't do a
    > check for it. What I really need to do is alter the session variable as
    > soon as they update the checkbox.[/color]

    Instead of checking checkboxes within the $_POST array do that within
    the $_SESSION array

    <?php
    foreach ($_SESSION['checkbox'] as $k=>$dummy) {
    if (isset($_POST[$k])) $_SESSION['checkbox'][$k] = true;
    else $_SESSION['checkbox'][$k] = false;
    }
    ?>

    Just remember to name your HTML checkboxes the same as your
    $_SESSION['checkbox'] indexes




    <?php // form output
    $_SESSION['checkbox']['cb1'] = true;
    echo '<input type="checkbox" name="cb1" checked>';
    // ...
    ?>
    --
    --= my mail box only accepts =--
    --= Content-Type: text/plain =--
    --= Size below 10001 bytes =--

    Comment

    Working...