How to post multiple as array in PHP?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







19















So that in PHP I can deal with them as :



foreach($_POST['checkboxname'] as $i => $value)
...









share|improve this question





























    19















    So that in PHP I can deal with them as :



    foreach($_POST['checkboxname'] as $i => $value)
    ...









    share|improve this question

























      19












      19








      19








      So that in PHP I can deal with them as :



      foreach($_POST['checkboxname'] as $i => $value)
      ...









      share|improve this question














      So that in PHP I can deal with them as :



      foreach($_POST['checkboxname'] as $i => $value)
      ...






      php html






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 30 '09 at 6:52









      user198729user198729

      23.1k91223323




      23.1k91223323
























          5 Answers
          5






          active

          oldest

          votes


















          36














          Do something like this:



          <input type="checkbox" name="checkboxArray" />


          Note the in the name.






          share|improve this answer



















          • 13





            +1 Great answer. @unknown Just remember that if none of them are checked, the field won't even be submitted causing the foreach to fail. Be sure to test isset($_POST['checkboxname']) prior to the foreach.

            – Doug Neiner
            Dec 30 '09 at 6:58





















          12














          Like this:



          <input type="checkbox" name="checkboxname" />
          <input type="checkbox" name="checkboxname" />
          <input type="checkbox" name="checkboxname" />
          <input type="checkbox" name="checkboxname" />
          <input type="checkbox" name="checkboxname" />


          Just append to their names.






          share|improve this answer



















          • 3





            There is a little problem !! if i check 5th check box it should be some thing like Array ( [4] => on ) but it display Array ( [0] => on ) why it is should i please values like checkboxname[1] and checkboxname[2]???

            – Bilal Maqsood
            Sep 26 '15 at 17:07





















          3














          If you use an array for the checkboxes, you should add a value option as identifier for the single checkboxes, because then the returned array changes from
          Array ( [0] => on, [1] => on) to Array ( [0] => value1, [1] => value5 ), which let you identify the checked checkboxes.






          share|improve this answer































            1














            for those HTML form elements that can send multiple values to server (like checkboxes, or multiple select boxes), you should use an array like name for your HTML element name. like this:



            <input type="checkbox" name="checkboxname" />


            also it is recommended that you use an enctype of "multipart/form-data" for your form element.



            <form enctype="multipart/form-data" action="target.php" method="post">


            then in your PHP scripts you can access the multiple values data as an array, just like you wanted.






            share|improve this answer
























            • Seems enctype is unneccesary.

              – user198729
              Dec 30 '09 at 7:02



















            0

















            <html>
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
            <script>
            $(document).ready(function(even){
            $("button").click(function(){
            var checkvalue = ;
            $.each($("input[name='1']:checked"), function(){
            checkvalue.push($(this).val());
            });
            alert("checkvalue: " + checkvalue.join(", "));
            });
            });
            </script>
            <body>
            <input type="checkbox" name="1" value="1" > 1 <br/>
            <input type="checkbox" name="1" value="2"> 2 <br/>
            <input type="checkbox" name="1" value="3"> 3 <br/>
            <button type="button">Get Values</button>
            </body>
            </html>








            share|improve this answer
























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f1978781%2fhow-to-post-multiple-input-type-checkbox-as-array-in-php%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              5 Answers
              5






              active

              oldest

              votes








              5 Answers
              5






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              36














              Do something like this:



              <input type="checkbox" name="checkboxArray" />


              Note the in the name.






              share|improve this answer



















              • 13





                +1 Great answer. @unknown Just remember that if none of them are checked, the field won't even be submitted causing the foreach to fail. Be sure to test isset($_POST['checkboxname']) prior to the foreach.

                – Doug Neiner
                Dec 30 '09 at 6:58


















              36














              Do something like this:



              <input type="checkbox" name="checkboxArray" />


              Note the in the name.






              share|improve this answer



















              • 13





                +1 Great answer. @unknown Just remember that if none of them are checked, the field won't even be submitted causing the foreach to fail. Be sure to test isset($_POST['checkboxname']) prior to the foreach.

                – Doug Neiner
                Dec 30 '09 at 6:58
















              36












              36








              36







              Do something like this:



              <input type="checkbox" name="checkboxArray" />


              Note the in the name.






              share|improve this answer













              Do something like this:



              <input type="checkbox" name="checkboxArray" />


              Note the in the name.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Dec 30 '09 at 6:54









              KramerCKramerC

              922815




              922815








              • 13





                +1 Great answer. @unknown Just remember that if none of them are checked, the field won't even be submitted causing the foreach to fail. Be sure to test isset($_POST['checkboxname']) prior to the foreach.

                – Doug Neiner
                Dec 30 '09 at 6:58
















              • 13





                +1 Great answer. @unknown Just remember that if none of them are checked, the field won't even be submitted causing the foreach to fail. Be sure to test isset($_POST['checkboxname']) prior to the foreach.

                – Doug Neiner
                Dec 30 '09 at 6:58










              13




              13





              +1 Great answer. @unknown Just remember that if none of them are checked, the field won't even be submitted causing the foreach to fail. Be sure to test isset($_POST['checkboxname']) prior to the foreach.

              – Doug Neiner
              Dec 30 '09 at 6:58







              +1 Great answer. @unknown Just remember that if none of them are checked, the field won't even be submitted causing the foreach to fail. Be sure to test isset($_POST['checkboxname']) prior to the foreach.

              – Doug Neiner
              Dec 30 '09 at 6:58















              12














              Like this:



              <input type="checkbox" name="checkboxname" />
              <input type="checkbox" name="checkboxname" />
              <input type="checkbox" name="checkboxname" />
              <input type="checkbox" name="checkboxname" />
              <input type="checkbox" name="checkboxname" />


              Just append to their names.






              share|improve this answer



















              • 3





                There is a little problem !! if i check 5th check box it should be some thing like Array ( [4] => on ) but it display Array ( [0] => on ) why it is should i please values like checkboxname[1] and checkboxname[2]???

                – Bilal Maqsood
                Sep 26 '15 at 17:07


















              12














              Like this:



              <input type="checkbox" name="checkboxname" />
              <input type="checkbox" name="checkboxname" />
              <input type="checkbox" name="checkboxname" />
              <input type="checkbox" name="checkboxname" />
              <input type="checkbox" name="checkboxname" />


              Just append to their names.






              share|improve this answer



















              • 3





                There is a little problem !! if i check 5th check box it should be some thing like Array ( [4] => on ) but it display Array ( [0] => on ) why it is should i please values like checkboxname[1] and checkboxname[2]???

                – Bilal Maqsood
                Sep 26 '15 at 17:07
















              12












              12








              12







              Like this:



              <input type="checkbox" name="checkboxname" />
              <input type="checkbox" name="checkboxname" />
              <input type="checkbox" name="checkboxname" />
              <input type="checkbox" name="checkboxname" />
              <input type="checkbox" name="checkboxname" />


              Just append to their names.






              share|improve this answer













              Like this:



              <input type="checkbox" name="checkboxname" />
              <input type="checkbox" name="checkboxname" />
              <input type="checkbox" name="checkboxname" />
              <input type="checkbox" name="checkboxname" />
              <input type="checkbox" name="checkboxname" />


              Just append to their names.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Dec 30 '09 at 6:54









              SarfrazSarfraz

              304k65474549




              304k65474549








              • 3





                There is a little problem !! if i check 5th check box it should be some thing like Array ( [4] => on ) but it display Array ( [0] => on ) why it is should i please values like checkboxname[1] and checkboxname[2]???

                – Bilal Maqsood
                Sep 26 '15 at 17:07
















              • 3





                There is a little problem !! if i check 5th check box it should be some thing like Array ( [4] => on ) but it display Array ( [0] => on ) why it is should i please values like checkboxname[1] and checkboxname[2]???

                – Bilal Maqsood
                Sep 26 '15 at 17:07










              3




              3





              There is a little problem !! if i check 5th check box it should be some thing like Array ( [4] => on ) but it display Array ( [0] => on ) why it is should i please values like checkboxname[1] and checkboxname[2]???

              – Bilal Maqsood
              Sep 26 '15 at 17:07







              There is a little problem !! if i check 5th check box it should be some thing like Array ( [4] => on ) but it display Array ( [0] => on ) why it is should i please values like checkboxname[1] and checkboxname[2]???

              – Bilal Maqsood
              Sep 26 '15 at 17:07













              3














              If you use an array for the checkboxes, you should add a value option as identifier for the single checkboxes, because then the returned array changes from
              Array ( [0] => on, [1] => on) to Array ( [0] => value1, [1] => value5 ), which let you identify the checked checkboxes.






              share|improve this answer




























                3














                If you use an array for the checkboxes, you should add a value option as identifier for the single checkboxes, because then the returned array changes from
                Array ( [0] => on, [1] => on) to Array ( [0] => value1, [1] => value5 ), which let you identify the checked checkboxes.






                share|improve this answer


























                  3












                  3








                  3







                  If you use an array for the checkboxes, you should add a value option as identifier for the single checkboxes, because then the returned array changes from
                  Array ( [0] => on, [1] => on) to Array ( [0] => value1, [1] => value5 ), which let you identify the checked checkboxes.






                  share|improve this answer













                  If you use an array for the checkboxes, you should add a value option as identifier for the single checkboxes, because then the returned array changes from
                  Array ( [0] => on, [1] => on) to Array ( [0] => value1, [1] => value5 ), which let you identify the checked checkboxes.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 19 '17 at 13:31









                  JestaBluntJestaBlunt

                  5810




                  5810























                      1














                      for those HTML form elements that can send multiple values to server (like checkboxes, or multiple select boxes), you should use an array like name for your HTML element name. like this:



                      <input type="checkbox" name="checkboxname" />


                      also it is recommended that you use an enctype of "multipart/form-data" for your form element.



                      <form enctype="multipart/form-data" action="target.php" method="post">


                      then in your PHP scripts you can access the multiple values data as an array, just like you wanted.






                      share|improve this answer
























                      • Seems enctype is unneccesary.

                        – user198729
                        Dec 30 '09 at 7:02
















                      1














                      for those HTML form elements that can send multiple values to server (like checkboxes, or multiple select boxes), you should use an array like name for your HTML element name. like this:



                      <input type="checkbox" name="checkboxname" />


                      also it is recommended that you use an enctype of "multipart/form-data" for your form element.



                      <form enctype="multipart/form-data" action="target.php" method="post">


                      then in your PHP scripts you can access the multiple values data as an array, just like you wanted.






                      share|improve this answer
























                      • Seems enctype is unneccesary.

                        – user198729
                        Dec 30 '09 at 7:02














                      1












                      1








                      1







                      for those HTML form elements that can send multiple values to server (like checkboxes, or multiple select boxes), you should use an array like name for your HTML element name. like this:



                      <input type="checkbox" name="checkboxname" />


                      also it is recommended that you use an enctype of "multipart/form-data" for your form element.



                      <form enctype="multipart/form-data" action="target.php" method="post">


                      then in your PHP scripts you can access the multiple values data as an array, just like you wanted.






                      share|improve this answer













                      for those HTML form elements that can send multiple values to server (like checkboxes, or multiple select boxes), you should use an array like name for your HTML element name. like this:



                      <input type="checkbox" name="checkboxname" />


                      also it is recommended that you use an enctype of "multipart/form-data" for your form element.



                      <form enctype="multipart/form-data" action="target.php" method="post">


                      then in your PHP scripts you can access the multiple values data as an array, just like you wanted.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Dec 30 '09 at 6:58









                      farzadfarzad

                      7,26952738




                      7,26952738













                      • Seems enctype is unneccesary.

                        – user198729
                        Dec 30 '09 at 7:02



















                      • Seems enctype is unneccesary.

                        – user198729
                        Dec 30 '09 at 7:02

















                      Seems enctype is unneccesary.

                      – user198729
                      Dec 30 '09 at 7:02





                      Seems enctype is unneccesary.

                      – user198729
                      Dec 30 '09 at 7:02











                      0

















                      <html>
                      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
                      <script>
                      $(document).ready(function(even){
                      $("button").click(function(){
                      var checkvalue = ;
                      $.each($("input[name='1']:checked"), function(){
                      checkvalue.push($(this).val());
                      });
                      alert("checkvalue: " + checkvalue.join(", "));
                      });
                      });
                      </script>
                      <body>
                      <input type="checkbox" name="1" value="1" > 1 <br/>
                      <input type="checkbox" name="1" value="2"> 2 <br/>
                      <input type="checkbox" name="1" value="3"> 3 <br/>
                      <button type="button">Get Values</button>
                      </body>
                      </html>








                      share|improve this answer




























                        0

















                        <html>
                        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
                        <script>
                        $(document).ready(function(even){
                        $("button").click(function(){
                        var checkvalue = ;
                        $.each($("input[name='1']:checked"), function(){
                        checkvalue.push($(this).val());
                        });
                        alert("checkvalue: " + checkvalue.join(", "));
                        });
                        });
                        </script>
                        <body>
                        <input type="checkbox" name="1" value="1" > 1 <br/>
                        <input type="checkbox" name="1" value="2"> 2 <br/>
                        <input type="checkbox" name="1" value="3"> 3 <br/>
                        <button type="button">Get Values</button>
                        </body>
                        </html>








                        share|improve this answer


























                          0












                          0








                          0










                          <html>
                          <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
                          <script>
                          $(document).ready(function(even){
                          $("button").click(function(){
                          var checkvalue = ;
                          $.each($("input[name='1']:checked"), function(){
                          checkvalue.push($(this).val());
                          });
                          alert("checkvalue: " + checkvalue.join(", "));
                          });
                          });
                          </script>
                          <body>
                          <input type="checkbox" name="1" value="1" > 1 <br/>
                          <input type="checkbox" name="1" value="2"> 2 <br/>
                          <input type="checkbox" name="1" value="3"> 3 <br/>
                          <button type="button">Get Values</button>
                          </body>
                          </html>








                          share|improve this answer
















                          <html>
                          <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
                          <script>
                          $(document).ready(function(even){
                          $("button").click(function(){
                          var checkvalue = ;
                          $.each($("input[name='1']:checked"), function(){
                          checkvalue.push($(this).val());
                          });
                          alert("checkvalue: " + checkvalue.join(", "));
                          });
                          });
                          </script>
                          <body>
                          <input type="checkbox" name="1" value="1" > 1 <br/>
                          <input type="checkbox" name="1" value="2"> 2 <br/>
                          <input type="checkbox" name="1" value="3"> 3 <br/>
                          <button type="button">Get Values</button>
                          </body>
                          </html>








                          <html>
                          <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
                          <script>
                          $(document).ready(function(even){
                          $("button").click(function(){
                          var checkvalue = ;
                          $.each($("input[name='1']:checked"), function(){
                          checkvalue.push($(this).val());
                          });
                          alert("checkvalue: " + checkvalue.join(", "));
                          });
                          });
                          </script>
                          <body>
                          <input type="checkbox" name="1" value="1" > 1 <br/>
                          <input type="checkbox" name="1" value="2"> 2 <br/>
                          <input type="checkbox" name="1" value="3"> 3 <br/>
                          <button type="button">Get Values</button>
                          </body>
                          </html>





                          <html>
                          <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
                          <script>
                          $(document).ready(function(even){
                          $("button").click(function(){
                          var checkvalue = ;
                          $.each($("input[name='1']:checked"), function(){
                          checkvalue.push($(this).val());
                          });
                          alert("checkvalue: " + checkvalue.join(", "));
                          });
                          });
                          </script>
                          <body>
                          <input type="checkbox" name="1" value="1" > 1 <br/>
                          <input type="checkbox" name="1" value="2"> 2 <br/>
                          <input type="checkbox" name="1" value="3"> 3 <br/>
                          <button type="button">Get Values</button>
                          </body>
                          </html>






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 16 '18 at 12:43









                          sakthi sudhansakthi sudhan

                          655




                          655






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f1978781%2fhow-to-post-multiple-input-type-checkbox-as-array-in-php%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Bressuire

                              Vorschmack

                              Quarantine