title: Passing javascript/Jquery array to php through a form
body: <p>So I have a jquery function that checks what checkboxes are checked and puts the id's in an array. Now that array gets passed to the value of a hidden input field and later submitted to the php script. What happens is, it looks like it puts every value in the first index instead of individual indexes.</p>
  
  <p>This is the function for getting the id's of the checked checkboxes in the array. I read about push and so far I understood that push actually pushes a value to a new index?</p>
  
  <pre><code>$(".article-checkbox:checked").each(function() {
              multipleCheckboxes.push($(this).val());
          });
  </code></pre>
  
  <p>My form:</p>
  
  <pre><code>&lt;form id="publish-form" method="post" action="publisharticle.php" onsubmit="getMultipleCheckboxValues()"&gt;
      &lt;input type="hidden" value="" name="checkbox-value2[]" id="checkbox-value2"&gt;
      li&gt;&lt;input type="submit" value="Publish"&gt;&lt;/li&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>the function onsubmit:</p>
  
  <pre><code>function getMultipleCheckboxValues(){
      getValueUsingClass();
      $("#checkbox-value2").val(multipleCheckboxes);
  }
  
  function getValueUsingClass(){
          /* declare an checkbox array */
          var chkArray = [];
          var i = 0;
  
          /* look for all checkboes that have a class 'chk' attached to it and check if it was checked */
          $(".article-checkbox:checked").each(function() {
              chkArray.push($(this).val());
              multipleCheckboxes.push($(this).val());
              i++;
          });
  
          /* we join the array separated by the comma */
          var selectedOne = chkArray[0];
          var selected;
          selected = chkArray.join(',') + ",";
  
          /* check if there is selected checkboxes, by default the length is 1 as it contains one single comma */
          if(selected.length &gt; 1){
              oneCheckbox = selectedOne;
          //alert("Please at least one of the checkbox" + selectedCheckboxes); 
      }else{
          alert("Please at least one of the checkbox");   
      }
  }
  </code></pre>
  
  <p>and php:</p>
  
  <pre><code>$checks =  $_POST['checkbox-value2'];
  
  echo $checks[0];
  </code></pre>
  
  <p>Now in PHP I want to iterate through an array instead of a string. Lets say I check checkbox one and two the php code above echo's 1,2 even though I chose index 0. I also tried:</p>
  
  <pre><code>var i = 0;
  $(".article-checkbox:checked").each(function() {
              chkArray.push($(this).val());
              multipleCheckboxes[i].push($(this).val());
              i++;
          });
  </code></pre>