Checkboxs and array indices

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

    Checkboxs and array indices

    I am having a problem with checkboxes...

    I have a number of blocks like this on my form:

    <input type="text" name="name[]" value="a name">
    <input type="text" name="ddi[]" value="1234">
    <input type="checkbox" name="delete[]">

    If I then submit the form, the array $delete is the only one which isn't
    indexed by its order on the form.

    For example, $name[ 0 ] and $ddi[ 0 ] always return the value of the first
    name[] and ddi[] input on the form.

    If I check any one of the checkboxes, say the third one, then $delete[ 0 ]
    will be "on", and all of the other including $delete[ 2 ] will be off.

    If I checked the third and seventh checkbox, just as an example, $delete[
    0 ] and $delete[ 1 ] will be "on".

    So, there is no way I can tell which checkboxes are actually checked, just
    how many have been checked. This is extremely annoying and doesn't happen
    with any of the other form controls. Any ideas?

    --
    "Come to think of it, there are already a million monkeys on a million
    typewriters, and the Usenet is NOTHING like Shakespeare!" - Blair Houghton
    -=-=-=-=-=-=-=-=-=-=-=-

    -=-=-=-=-=-=-=-=-=-=-=-


  • Nik Coughin

    #2
    Re: Checkboxs and array indices

    Nik Coughin wrote:[color=blue]
    > I am having a problem with checkboxes...
    >
    > I have a number of blocks like this on my form:
    >
    > <input type="text" name="name[]" value="a name">
    > <input type="text" name="ddi[]" value="1234">
    > <input type="checkbox" name="delete[]">
    >
    > If I then submit the form, the array $delete is the only one which
    > isn't indexed by its order on the form.
    >
    > For example, $name[ 0 ] and $ddi[ 0 ] always return the value of the
    > first name[] and ddi[] input on the form.
    >
    > If I check any one of the checkboxes, say the third one, then
    > $delete[ 0 ] will be "on", and all of the other including $delete[ 2
    > ] will be off.
    > If I checked the third and seventh checkbox, just as an example,
    > $delete[ 0 ] and $delete[ 1 ] will be "on".
    >
    > So, there is no way I can tell which checkboxes are actually checked,
    > just how many have been checked. This is extremely annoying and
    > doesn't happen with any of the other form controls. Any ideas?[/color]

    Figured it out. You have to give the checkboxes a value and check for the
    value. It's not so simple as checking if a given checkbox returns "on".
    Though why on Earth it does what I've described above when you omit the
    value is beyond me.


    Comment

    • Chris Hope

      #3
      Re: Checkboxs and array indices

      Nik Coughin wrote:
      [color=blue]
      > I am having a problem with checkboxes...
      >
      > I have a number of blocks like this on my form:
      >
      > <input type="text" name="name[]" value="a name">
      > <input type="text" name="ddi[]" value="1234">
      > <input type="checkbox" name="delete[]">
      >
      > If I then submit the form, the array $delete is the only one which
      > isn't indexed by its order on the form.
      >
      > For example, $name[ 0 ] and $ddi[ 0 ] always return the value of the
      > first name[] and ddi[] input on the form.
      >
      > If I check any one of the checkboxes, say the third one, then $delete[
      > 0 ] will be "on", and all of the other including $delete[ 2 ] will be
      > off.
      >
      > If I checked the third and seventh checkbox, just as an example,
      > $delete[ 0 ] and $delete[ 1 ] will be "on".
      >
      > So, there is no way I can tell which checkboxes are actually checked,
      > just
      > how many have been checked. This is extremely annoying and doesn't
      > happen
      > with any of the other form controls. Any ideas?[/color]

      It's because checkboxes aren't submitted by the browser if there aren't
      checked. PHP has no way of knowing there were eg 5 checkboxes on the
      form all named delete[] and that only 2 were submitted; it just sees
      the two that were submitted.

      Another way to do it is like so, where you explicitly name the
      checkboxes with an incrementing number:

      <input type="checkbox" name="delete[0]">
      <input type="checkbox" name="delete[1]">
      <input type="checkbox" name="delete[2]">

      This should then give you the behaviour you are expecting.

      In the example above, if you checked the first and last ones, using
      print_r($_POST) you would get the following:

      [delete] => Array
      (
      [0] => on
      [2] => on
      )

      Note that [1] is not set because it was not posted.

      --
      Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/

      Comment

      • Chris Hope

        #4
        Re: Checkboxs and array indices

        Nik Coughin wrote:
        [color=blue]
        > Nik Coughin wrote:[color=green]
        >> I am having a problem with checkboxes...
        >>
        >> I have a number of blocks like this on my form:
        >>
        >> <input type="text" name="name[]" value="a name">
        >> <input type="text" name="ddi[]" value="1234">
        >> <input type="checkbox" name="delete[]">
        >>
        >> If I then submit the form, the array $delete is the only one which
        >> isn't indexed by its order on the form.
        >>
        >> For example, $name[ 0 ] and $ddi[ 0 ] always return the value of the
        >> first name[] and ddi[] input on the form.
        >>
        >> If I check any one of the checkboxes, say the third one, then
        >> $delete[ 0 ] will be "on", and all of the other including $delete[ 2
        >> ] will be off.
        >> If I checked the third and seventh checkbox, just as an example,
        >> $delete[ 0 ] and $delete[ 1 ] will be "on".
        >>
        >> So, there is no way I can tell which checkboxes are actually checked,
        >> just how many have been checked. This is extremely annoying and
        >> doesn't happen with any of the other form controls. Any ideas?[/color]
        >
        > Figured it out. You have to give the checkboxes a value and check for
        > the
        > value. It's not so simple as checking if a given checkbox returns
        > "on". Though why on Earth it does what I've described above when you
        > omit the value is beyond me.[/color]

        Have a read my other post. The reason it does it the way is does, is
        because the data is not sent from the browser in the first place if the
        checkbox is not checked.

        Consider the following when you have actually assigned a value to the
        checkbox:

        Select your favourite activites from the following:
        <input type="checkbox" name="foo[]" value="swimming ">Swimming
        <input type="checkbox" name="foo[]" value="cycling" >Cycling
        <input type="checkbox" name="foo[]" value="running" >Running
        <input type="checkbox" name="foo[]" value="skiing"> Skiing

        The user is supposed to select zero or more options from the list. If
        the user only checked two of the four options do you think it should
        still submit all the options? If it did, how would your server sided
        script know which was checked?

        --
        Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/

        Comment

        Working...