Problems sending arrays by form when type=checkbox

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

    Problems sending arrays by form when type=checkbox

    Hi all, I have a problem when I try to send an array using a form when
    the type="checkbox" .

    This is my form input row:

    <INPUT type="Checkbox" name="flg[]" value="y" <? if($row['flag'] ==
    'y') echo 'CHECKED'; ?>>

    Since this is an update form, I set the status of the flg[] according
    to the field "flag" stored in a MySQL database. The user may check or
    uncheck each of the checkboxes on the form.

    This form is sent to another PHP file with other fileds of type
    <SELECT> and type="Text". Here's the code which process the checkbox
    input:

    $max_i = count($flg);
    for($i=0;$i<$ma x_i;$i++)
    {
    if($flg[$i] == 'y')
    {
    $flag = 'y';
    }
    else
    {
    $flag = 'n';
    }
    echo $i.'-->'.$flg[$i].'-->'.$flag.'<BR>' ;
    }

    I have no problems processing the array associated with the SELECT
    and "Text" fileds, but when I try using the Checkbox, this is what I
    receive:

    0-->y-->y
    1-->y-->y
    2-->y-->y
    3-->y-->y
    4-->y-->y
    5-->y-->y

    Trying with this other code:

    $num_of_checkbo x_fileds = 12;

    for($i=0;$i<$nu m_of_checkbox_f ileds;$i++)
    {
    if($flg[$i] == 'y')
    {
    $flag = 'y';
    }
    else
    {
    $flag = 'n';
    }
    echo $i.'-->'.$flg[$i].'-->'.$flag.'<BR>' ;
    }

    I receive this output:

    0-->y-->y
    1-->y-->y
    2-->y-->y
    3-->y-->y
    4-->y-->y
    5-->y-->y
    6-->-->n
    7-->-->n
    8-->-->n
    9-->-->n
    10-->-->n
    11-->-->n


    The problem is that there is no order in what I receive!!
    I have the first elements af the array with a 'y' and the others with
    a 'n'.
    The correct output, according with the user's input, should be:

    0-->n-->n
    1-->y-->y
    2-->n-->n
    3-->y-->y
    4-->n-->n
    5-->y-->y
    6-->n-->n
    7-->y-->y
    8-->n-->n
    9-->y-->y
    10-->n-->n
    11-->y-->y

    Any idea? Please help!!!

    Thank You.

  • Pedro Graca

    #2
    Re: Problems sending arrays by form when type=checkbox

    Old Lady wrote:[color=blue]
    > I have a problem when I try to send an array using a form when
    > the type="checkbox" .
    >
    > This is my form input row:
    >
    > <INPUT type="Checkbox" name="flg[]" value="y" <? if($row['flag'] ==
    > 'y') echo 'CHECKED'; ?>>[/color]

    Give each checkbox a different index

    <input type="checkbox" name="flg[0]" ... />
    <input type="checkbox" name="flg[1]" ... />
    ...

    This way you can access them as a set in php ($_POST['flg']) or
    individually ($_POST['flg'][2]).




    BTW: Don't you want to turn register_global s off? :-)

    --
    Mail to my "From:" address is readable by all at http://www.dodgeit.com/
    == ** ## !! ------------------------------------------------ !! ## ** ==
    TEXT-ONLY mail to the whole "Reply-To:" address ("My Name" <my@address>)
    may bypass my spam filter. If it does, I may reply from another address!

    Comment

    • Old Lady

      #3
      Re: Problems sending arrays by form when type=checkbox

      >Give each checkbox a different index[color=blue]
      >
      > <input type="checkbox" name="flg[0]" ... />
      > <input type="checkbox" name="flg[1]" ... />
      > ...
      >
      >This way you can access them as a set in php ($_POST['flg']) or
      >individually ($_POST['flg'][2]).[/color]

      Yes, I already tryied this and it works, but I am trying to understand
      why this problem is just with checkboxes and not with text or select
      fields. Any idea? It's a HTML bug or what?
      [color=blue]
      >BTW: Don't you want to turn register_global s off? :-)[/color]

      Yep... But the code is VERY old and I have to change a lot of rows in
      a suite applications used by 2,500 users. I'm starting fixing the code
      for the register_global s, but this will take A LOT of time, to fix and
      to check for any new errors...


      Comment

      • scotty

        #4
        Re: Problems sending arrays by form when type=checkbox

        Don't know exactly what your are trying to do but:

        When a user checks the checkbox, only the items checked show up as
        array items based upon your form. So a possible 'no' related to an
        unchecked box at position 6 will be overwritten with a checked 'yes'
        from position 8 based upon the way you write your form, so you will not
        have the correct resulting order at the end. When you later go through
        and compare, your 'no's may not be in the correct order as a result.
        You could change the value yes to a incrementing number. Then, when
        you examine the array, you at least know which items belong where in
        the output, and you write your output accordingly.

        Any items that were checked mean yes to you, so it doesn't matter if
        there 'value' is a number to help you order them later.

        Scott

        Old Lady wrote:[color=blue][color=green]
        > >Give each checkbox a different index
        > >
        > > <input type="checkbox" name="flg[0]" ... />
        > > <input type="checkbox" name="flg[1]" ... />
        > > ...
        > >
        > >This way you can access them as a set in php ($_POST['flg']) or
        > >individually ($_POST['flg'][2]).[/color]
        >
        > Yes, I already tryied this and it works, but I am trying to[/color]
        understand[color=blue]
        > why this problem is just with checkboxes and not with text or select
        > fields. Any idea? It's a HTML bug or what?
        >[color=green]
        > >BTW: Don't you want to turn register_global s off? :-)[/color]
        >
        > Yep... But the code is VERY old and I have to change a lot of rows in
        > a suite applications used by 2,500 users. I'm starting fixing the[/color]
        code[color=blue]
        > for the register_global s, but this will take A LOT of time, to fix[/color]
        and[color=blue]
        > to check for any new errors...[/color]

        Comment

        • Michael Fesser

          #5
          Re: Problems sending arrays by form when type=checkbox

          .oO(Old Lady)
          [color=blue][color=green]
          >>This way you can access them as a set in php ($_POST['flg']) or
          >>individuall y ($_POST['flg'][2]).[/color]
          >
          >Yes, I already tryied this and it works, but I am trying to understand
          >why this problem is just with checkboxes and not with text or select
          >fields. Any idea? It's a HTML bug or what?[/color]

          It's not a bug, it's a feature (or simply the way HTML works).

          17.13.2 Successful controls


          Unchecked checkboxes are not considered successful and hence not
          submitted to the server. Without explicitly indexing them the result
          will be rather random and useless.

          Micha

          Comment

          • Pedro Graca

            #6
            Re: Problems sending arrays by form when type=checkbox

            Old Lady wrote:[color=blue]
            > I am trying to understand
            > why this problem is just with checkboxes and not with text or select
            > fields. Any idea? It's a HTML bug or what?[/color]

            By design (HTML) only form controls with data get sent to the server.
            So, if you have

            [X] check 1
            [ ] check 2
            [X] check 3

            Only "check 1" and "check 3" will be sent to the server.
            When the control names are equal and include "[]" php converts them to
            an array.
            The browser sends something like "flg[]; flg[]" and php converts
            to flg[0] and flg[1].

            If you include the indexes in the control name, the browser will send
            "flg[0]; flg[2]" which will be interpreted literally by php.

            And that means that if you do

            <?php
            for ($i=0; $i<3; ++$i) {
            if ($_POST['flg'][$i]) /* something */;
            }

            you will get a notice about an undefined index for $_POST['flg'][1].

            [color=blue][color=green]
            >>BTW: Don't you want to turn register_global s off? :-)[/color]
            >
            > Yep... But the code is VERY old and I have to change a lot of rows in
            > a suite applications used by 2,500 users. I'm starting fixing the code
            > for the register_global s, but this will take A LOT of time, to fix and
            > to check for any new errors...[/color]

            I don't envy you :-)
            Good luck!

            --
            Mail to my "From:" address is readable by all at http://www.dodgeit.com/
            == ** ## !! ------------------------------------------------ !! ## ** ==
            TEXT-ONLY mail to the whole "Reply-To:" address ("My Name" <my@address>)
            may bypass my spam filter. If it does, I may reply from another address!

            Comment

            • Old Lady

              #7
              Re: Problems sending arrays by form when type=checkbox

              Thank You Scott and Michael for your answers.

              This is what I needed: knowing why the input order was not respected.
              This means that when one want to use checkboxes in an array, he/she
              HAVE to think a workaround.
              Actually the workaround which best solve my needs is to 'force' the
              array items using flg[0], flg[1], ..., flg[n] directly in the input
              form. This is a very easy task to do in PHP, so this is the workaround
              I used.

              I also used the other workaround proposed by Scott in another
              procedure, since it was best for another kind, but similar,
              application.

              Thank You again for your fast answers! ;-)

              Old Lady

              Comment

              • Geoff Berrow

                #8
                Re: Problems sending arrays by form when type=checkbox

                I noticed that Message-ID:
                <1102947095.798 834.29050@z14g2 000cwz.googlegr oups.com> from scotty
                contained the following:
                [color=blue]
                >When you later go through
                >and compare, your 'no's may not be in the correct order as a result.
                >You could change the value yes to a incrementing number.[/color]

                Or even the name of the item

                --
                Geoff Berrow (put thecat out to email)
                It's only Usenet, no one dies.
                My opinions, not the committee's, mine.
                Simple RFDs http://www.ckdog.co.uk/rfdmaker/

                Comment

                Working...