title: How to send array of JSON objects with jQuery
body: <p>I'm programming an web page and I really stucked trying to send an Array of JSON objects to my PHP backend script.</p>
  
  <p>this is my javascript code (using jQuery):</p>
  
  <pre><code>            var toSend = new Array();
              $("input[type=checkbox]").each(
                  function (indice, item)
                  {
                      var dom = $(item);
                      var domJSON = {
                          id: dom.attr("value"),
                          checked: (dom.attr("checked") == "checked" ? true : false)
                      };
                      //put object as JSON in array:
                      toSend.push($.toJSON(domJSON));                        
                  }
              );
              $.ajax({
                  type:"POST",
                  url: "salvar_escala.php",
                  data: {checkbox: toSend},
                  success: function(response) {
                      $("#div_salvar").html(response);
                  },
                  error: function() {
                      alert("Erro");
                  }
                }
              );
  </code></pre>
  
  <p>And in PHP I have this:</p>
  
  <pre><code>    //Grab the array
      $arrayFromAjax = $_POST['checkbox'];
  
      foreach($arrayFromAjax as $aux) {
          $temp = json_decode($aux, true);
          $id = $temp['id'];
          $value = $temp['checked'];
  
          //This line doesn't print anything  for $id and $value
          echo "Id: $id | Value: $value&lt;br /&gt;";
  
          //This line prints an crazy string, but with right values
          echo "CHEKBOX[] =&gt; $b&lt;br /&gt;";
      }
  </code></pre>
  
  <p>In this code, I'm decoding my objects to json, putting then in an array and sending. I also tried but objects in array (without json), and then, convert the array to json, and send them like that:</p>
  
  <pre><code>            $.ajax({
                  type:"POST",
                  url: "salvar_escala.php",
                  dataType: "json",
                  data: {checkbox: $.toJSON(toSend)},
                  success: function(response) {
                      $("#div_salvar").html(response);
                  },
                  error: function() {
                      alert("Erro");
                  }
                }
              );
  </code></pre>
  
  <p>But in this case, it's worse, the error function is called.</p>