Difficulty unsetting checkbox elements via session controls

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

    Difficulty unsetting checkbox elements via session controls

    Hi there - I'm experiencing a very frustrating problem with an
    application that submits POST info to a session control file which
    then echo's it back to the controls if the page is returned to.

    This works fine, except for check boxes - once it is checked, if I
    then then return and uncheck it, then again return to the page later
    on, the session still marks it as checked.

    The code on the main pages is:

    <input name="withdrawa ls_taken_10" type="checkbox" value="checkbox "
    <?php if (isset($_SESSIO N["withdrawals_ta ken_10"])) {echo 'checked';}
    else {echo '';} ?>>

    The code on the control page is:

    foreach($_REQUE ST as $key => $val) {
    $_SESSION[$key] = $val; }

    ....quite straightforward stuff, but once a checkbox is registered as
    set, why does it not register it as unchecked- especially if I leave
    them unchecked from the outset, the session will correctly show it as
    unchecked.

    Hmmm.......help .....

    Thanks
    Sylvian.
  • Pedro Graca

    #2
    Re: Difficulty unsetting checkbox elements via session controls

    Sylvian Stone wrote:[color=blue]
    > ...quite straightforward stuff, but once a checkbox is registered as
    > set, why does it not register it as unchecked- especially if I leave
    > them unchecked from the outset, the session will correctly show it as
    > unchecked.[/color]

    $_REQUEST will only have the selected elements in it.
    To clear the unselected elements you have to do it manually.
    Once you receive a checked value and put into into the session variable,
    with your current code there is no way it gets reset: the $_REQUEST
    array will never have the $key to an unchecked checkbox.

    #v+

    <?php
    $_SESSION['cbox1'] = '';
    $_SESSION['cbox2'] = '';
    // ...
    foreach ($_REQUEST as $key => $val) {
    $_SESSION[$key] = $val;
    }
    ?>

    #v-


    --
    --= my mail box only accepts =--
    --= Content-Type: text/plain =--
    --= Size below 10001 bytes =--

    Comment

    • Sylvian Stone

      #3
      Re: Difficulty unsetting checkbox elements via session controls

      Hi,

      The suggestion you gave below seems to have the opposite effect - in
      that every single checkbox becomes set when I return to the page, even
      if most were unchecked before the form submission.

      Sylvian.

      Pedro Graca <hexkid@hotpop. com> wrote in message news:<btjd2g$7s 9h2$2@ID-203069.news.uni-berlin.de>...[color=blue]
      > Sylvian Stone wrote:[color=green]
      > > ...quite straightforward stuff, but once a checkbox is registered as
      > > set, why does it not register it as unchecked- especially if I leave
      > > them unchecked from the outset, the session will correctly show it as
      > > unchecked.[/color]
      >
      > $_REQUEST will only have the selected elements in it.
      > To clear the unselected elements you have to do it manually.
      > Once you receive a checked value and put into into the session variable,
      > with your current code there is no way it gets reset: the $_REQUEST
      > array will never have the $key to an unchecked checkbox.
      >
      > #v+
      >
      > <?php
      > $_SESSION['cbox1'] = '';
      > $_SESSION['cbox2'] = '';
      > // ...
      > foreach ($_REQUEST as $key => $val) {
      > $_SESSION[$key] = $val;
      > }
      > ?>
      >
      > #v-[/color]

      Comment

      • Pedro Graca

        #4
        Re: Difficulty unsetting checkbox elements via session controls

        Sylvian Stone wrote:[color=blue]
        > The suggestion you gave below seems to have the opposite effect - in
        > that every single checkbox becomes set when I return to the page, even
        > if most were unchecked before the form submission.[/color]

        Sorry for the mess (I put myself into). I didn't pay attention to all of
        your code.

        Problem is that $_SESSION[] is tested with isset().
        With my suggestion all of the relevant $_SESSION[] are set, but their
        value is '' (the empty string).

        If you change the previous code from
        if (isset($_SESSIO N[...]))

        to
        if ($_SESSION[...] != '')

        I think it will be ok.


        All of the $_SESSION is guaranteed to exist, so there should be no need
        to test with isset(), but to be safer you may want to do
        if (isset($_SESSIO N[...]) && $_SESSION[...] != '')
        --
        --= my mail box only accepts =--
        --= Content-Type: text/plain =--
        --= Size below 10001 bytes =--

        Comment

        Working...