title: Send multiple checkbox data to PHP via jQuery ajax()
body: <p>I want to submit a POST form that contains a textarea field and an input field(s) (type="checkbox" with an arbitrary/variable number of checkboxes) on my website via jQuery's .ajax(). PHP receives the textarea data and the ajax response is correctly displayed to the user. However, it seems that PHP is not receiving the checkbox data (was it checked, or not). How can I get this to work? Here is the code I have:</p>
  
  <p>The HTML:</p>
  
  <pre><code>&lt;form method="post" action="myurl.php" id=myForm&gt;
      &lt;textarea id="myField" type="text" name="myField"&gt;&lt;/textarea&gt;
      &lt;input type="checkbox" name="myCheckboxes[]" id="myCheckboxes" value="someValue1" /&gt;
      &lt;input type="checkbox" name="myCheckboxes[]" id="myCheckboxes" value="someValue2" /&gt;
      ...(maybe some more checkboxes - dynamically generated as necessary)
      &lt;input id="submit" type="submit" name="submit" value="Submit" onclick="submitForm()" /&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>The jQuery:</p>
  
  <pre><code>function submitForm() {
  $(document).ready(function() {
  $("form#myForm").submit(function() {
  
          var myCheckboxes = new Array();
          $("input:checked").each(function() {
             myCheckboxes.push($(this).val());
          });
  
          $.ajax({
              type: "POST",
              url: "myurl.php",
              dataType: 'html',
              data: { myField:$("textarea[name=myField]").val(),
                      myCheckboxes:myCheckboxes },
              success: function(data){
                  $('#myResponse').html(data)
              }
          });
          return false;
  });
  });
  </code></pre>
  
  <p>Now, the PHP</p>
  
  <pre><code>$myField = htmlspecialchars( $_POST['myField'] ) );
  if( isset( $_POST['myCheckboxes'] ) )
  {
      for ( $i=0; $i &lt; count( $_POST['myCheckboxes'] ); $i++ )
      {
          // do some stuff, save to database, etc.
      }
  }
  // create the response
  $response = 'an HTML response';
  $response = stripslashes($response);
  echo($response);
  </code></pre>
  
  <p>Everything works great: when the form is submitted a new record is stored in my database, the response is ajaxed back to webpage, but the checkbox data is not sent. I want to know which, if any, of the checkboxes have been checked. I've read about .serialize(), JSON, etc, but none this has worked. Do I have to serialize/JSON in jQuery and PHP? How? Is one method better than another when sending form data with checkboxes? I've been stuck on this for 2 days. Any help would be greatly appreciated. Thanks ahead of time!</p>