Hi,

I have a problem evaluating a HTML Form like this:

<form method="post" action="action.php">
<input type=checkbox name="del" value="1">
<input type=checkbox name="del" value="2">
<input type=checkbox name="del" value="3">
<input type=checkbox name="del" value="4">
<input type=checkbox name="del" value="5">
<input type=checkbox name="del" value="6">
:
:
<input type=submit>
</form>

Assume that two or more items are checked.
When submitting the form I will always get only one variable $del with the last checked value. How can I get all checked item values (for example as an array of values) ?

Thanks,
Tom

    You solve the problem if use an array.
    Like this:
    ...
    <input type=checkbox name="del[1]" value="1">
    <input type=checkbox name="del[2]" value="2">
    <input type=checkbox name="del[3]" value="3">
    <input type=checkbox name="del[4]" value="4">
    <input type=checkbox name="del[5]" value="5">
    <input type=checkbox name="del[6]" value="6">
    ...

    /Metin A.

      A checkbox must have a unique name.
      Radiobuttons can have the same name to form a group.

      You can probably make it work using the solution that Metin A. offers,
      But the basic HTML is not correct.

        i'm not sure but i think you could use the same name of array
        Like this:

        <input type=checkbox name="del[]" value="1">
        <input type=checkbox name="del[]" value="2">
        <input type=checkbox name="del[]" value="3">
        <input type=checkbox name="del[]" value="4">
        <input type=checkbox name="del[]" value="5">
        <input type=checkbox name="del[]" value="6">

        the array will be automatically filled with the correct index ...

        hope it'll helpfull

          This method is perfectly okay:

          <input type=checkbox name="del[]" value="1">
          <input type=checkbox name="del[]" value="2">
          <input type=checkbox name="del[]" value="3">
          <input type=checkbox name="del[]" value="4">
          <input type=checkbox name="del[]" value="5">
          <input type=checkbox name="del[]" value="6">

          You can now access these elements either using a while or for loop.

          if (count($del) > 1) {
          for ($i=0;$i<count($del);$i++) {
          $result[$i] = mysql_query("DELETE FROM TABLENAME WHERE id = '$del[$i]'");

            }

          }

            Did you get this to work?

              Yes. This works fine for me.

              First I tried this:

              <input type=checkbox name="del[1]" value="1">
              <input type=checkbox name="del[2]" value="2">
              <input type=checkbox name="del[3]" value="3">
              <input type=checkbox name="del[4]" value="4">
              <input type=checkbox name="del[5]" value="5">
              <input type=checkbox name="del[6]" value="6">

              and it works fine. Than I tried this:

              <input type=checkbox name="del[]" value="1">
              <input type=checkbox name="del[]" value="2">
              <input type=checkbox name="del[]" value="3">
              <input type=checkbox name="del[]" value="4">
              <input type=checkbox name="del[]" value="5">
              <input type=checkbox name="del[]" value="6">

              and it works fine, too. I decided to use to second variant, because I don't care about array indices.

              Tom

                10 months later

                hello,

                I have a problem getting the values of checkboxes with the same name. I'm including the javascript functionality like uncheck and checkall. When I name my checkboxes as list (without the []) my javascript works, but when getting the values of the checked checkboxes, it only returns the last value. But when I name my checkboxes as list[], I'm able to get all the values of checked checkboxes but my javascript doesnt work. I saw an example of this, naming the checkboxes without the [] but they were able to get the values of the checked checkboxes.... I saw this in yahoo. they named their checkboxes as Mid without the [], but was able to get the values. Hope you can help me with my problem. 🙂

                  Hi,

                  the variable Name has to be with [] to get the results in an array.

                  To select all items I'm using this JS Function:

                  function all() {
                  var Msg = document.forms["list"];

                      for (var x = 0; x < Msg.length; x++)
                              if (typeof Msg.elements[x].checked != "undefined")
                                      Msg.elements[x].checked = true;

                  }

                  To unselect all items I'm simply reset to form.

                  Hope this helps,

                  Thomas

                    4 months later

                    I can make it work,

                    could you please write the whole again, include the JS script?

                    Regards

                    Johan

                      Generate your checkboxes like these:

                      <form method="post" action="whatever.php">
                      ...
                      <input type=checkbox name="del[]" value="1">
                      <input type=checkbox name="del[]" value="2">
                      ...
                      <input type=submit>
                      <input type=reset>
                      </form>

                      or, if you like predefined array indices:

                      <form method="post" action="whatever.php">
                      ...
                      <input type=checkbox name="del[1]" value="1">
                      <input type=checkbox name="del[2]" value="2">
                      ...
                      <input type=submit>
                      <input type=reset>
                      </form>

                      In whatever.php you will have an array-var named $del[] which contains the selected items.

                      To select all items I'm using this JS Function:

                      function all() {
                      var Msg = document.forms["yourframename"];

                      for (var x = 0; x < Msg.length; x++)
                      if (typeof Msg.elements[x].checked != "undefined")
                      Msg.elements[x].checked = true;
                      }

                      To unselect all items I'm simply reset the form.

                      Thomas

                        Hi,

                        How should I do if i want to selct all checkboxes and reset it with the same button?

                        I now use the a checkbox to mark all but if I "empty" the checkbox it should clear all selected.

                        Do you know how to do this?

                          I haven't done this for now, but you can try to do it like this: every time the "master" checkbox (which you use for check/uncheck all others) is clicked (use the onClick Event) read out the current value of the "master" checkbox and set the checked-Property of all other checkboxes to the same value than the first one.

                            Hi,

                            May seem stupid but just in case you dont get it to work, make sure the checkboxes are defined between the <form> and </form> tags...

                            grtz
                            MRJB

                              5 months later

                              var Msg = document.forms["yourframename"];

                              Instead of 'yourframename' it would be better to say 'yourformname'. I tried everything until I discoverd that it should be just that.

                                3 months later

                                I am a javascript inept idiot. Could someone actually show the HTML and JAVASCRIPT code that would be used in the onLick event?

                                Thanks

                                  Write a Reply...