title: How to use variables when creating JSON
body: <p>What I am trying to do is update my MySQL database from the result of an ajax call to a remote website and then use those results to update my own database.</p>
  
  <p>After building the JSON object by looping the result set using the PUSH method I console.log the result and get this :</p>
  
  <pre><code>[]
  0: {id: "3340", orderID: "1518883", status: "Shipped", shipdate: "March 12, 2020", shipfrom: "USA", …}
  1: {id: "1441", orderID: "1518884", status: "Scheduled", shipdate: "March 19, 2020", shipfrom: "USA", …}
  2: {id: "3345", orderID: "1518895", status: "Shipped", shipdate: "March 12, 2020", shipfrom: "IA", …}
  3: {id: "3342", orderID: "1518886", status: "Shipped", shipdate: "March 11, 2020", shipfrom: "TX", …}
  4: {id: "3346", orderID: "1518896", status: "Shipped", shipdate: "March 12, 2020", shipfrom: "MN", …}
  5: {id: "3343", orderID: "1518892", status: "Finishing", shipdate: "March 16, 2020", shipfrom: "IA", …}
  6: {id: "3344", orderID: "1518893", status: "Shipped", shipdate: "March 12, 2020", shipfrom: "IA", …}
  7: {id: "60", orderID: "1518887", status: "Shipped", shipdate: "March 12, 2020", shipfrom: "IA", …}
  8: {id: "2149", orderID: "1518888", status: "Shipped", shipdate: "March 12, 2020", shipfrom: "NV", …}
  9: {id: "2149", orderID: "1518894", status: "Shipped", shipdate: "March 12, 2020", shipfrom: "NV", …}
  length: 10
  __proto__: Array(0)
  </code></pre>
  
  <p>When I ajax it to PHP and var_dump it I get Array(0){}.  I have tried many ways from other peoples questions on this site with similar problems and this is the best result I have got, most results with other methods result in null or nothing at all.  </p>
  
  <p>I suspect it is all in the way I am constructing the JSON by using variables.  Here is the code I am using in js. When I paste the console result to lint I get errors.  I have tried sending test data to the php and get a result so I am fairly certain it is in the way I am preparing the JSON data.</p>
  
  <pre><code>$('#btnUpdate').on('click',
      function() {
  
          var jsonData = new Array();
          var oTable = $("#example").DataTable(); 
          oTable.rows().every(function (index, element) {
  
             // get tracking data
             var tableData = this.data(); 
              var gNum="https://tracking.xyz.com/api/?key=b39f008e318efd2bb988d724a161b61c6909677f&amp;order="+tableData[4];
  
  
          $.ajax({
                url: gNum,
                dataType: 'json',
              }).done(function(data) {
                  var data = JSON.stringify(data)
                  var mydata=(JSON.parse(data));
                  console.log(mydata);
                      if(mydata.order_info){
                          if(mydata.order_info.tracking){
                              jsObject = {
                              "id": tableData[1],
                              "orderID" : tableData[2],
                              "status": mydata.order_info.status,
                              "shipdate": mydata.order_info.ship_date,
                              "shipfrom": mydata.order_info.ship_from,
                              "track" : mydata.order_info.tracking.number,
                              "carrier": mydata.order_info.tracking.carrier
                              }
                          }else{
                              jsObject = {
                              "id": tableData[1],
                              "orderID" : tableData[2],
                              "status": mydata.order_info.status,
                              "shipdate": mydata.order_info.ship_date,
                              "shipfrom": mydata.order_info.ship_from,
                              "track" : "",
                              "carrier": ""
                              };  
                          };
                          JSON.stringify(jsObject);
  
                      jsonData.push(jsObject);    
                   }else{
                      alert('not found');
                  };
              });
  
          });
          console.log(jsonData)
          //var testData = new Array();
  //var record1 = {"var1":"9","var2":"16","var3":"16"};
  //var record2 = {"var4":"8","var5":"15","var6":"15"};
  //testData.push(record1);
  //testData.push(record2);
  
          // send data to server to update records
  $.ajax({
      type: 'POST',
      url: 'statusUpdate.php/',                      
      data: {'data': JSON.stringify(jsonData)},
      ContentType: 'application/x-www-form-urlencoded; charset=UTF-8',
      success: function(data) {
        alert(data);
      }
  });
  </code></pre>
  
  <p>PHP Code</p>
  
  <pre><code>&lt;?php
  
  $json= $_POST['data'];
  
  $array = json_decode( $json, true );
  var_dump($array);
  //exit;
  // getting no usable result so code below is not processed
  
  
  foreach ($array as $key =&gt; $jsons) { // This will search in the 2 jsons
       foreach($jsons as $key =&gt; $value) {
          if($key == 'id') $id = $value;
          if($key == 'orderID') $orderID = $value;
          if($key == 'status') $status = $value;
          if($key == 'shipdate') $shipdate = $value;
          if($key == 'shipfrom') $shipfrom = $value;
          if($key == 'track') $track = $value;
          if($key == 'carrier') $carrier = $value;
      $sql = "UPDATE Orders SET OrderStatus=" .$status. " shippedDate=" .$shipdate. " ShippedFrom =" .$shipfrom. " TrackingNumber = " .$track. " shippedVia =" .$carrier. " WHERE customer_id = " .$id. " AND OrderNumber = " . $orderID ;
      //if (!mysqli_query($con,$sql)) {
      //  echo("Error description: " . mysqli_error($con));
      //}
      }
  }
  echo $id;
  
  //database connection close
  mysqli_close($con); 
  
  ?&gt;
  </code></pre>
  
  <h2>Update</h2>
  
  <p>When I send hard coded test data the response is good so this is definitely an issue with the data collected from the previous ajax.  I read somewhere that the result of an ajax call is a reference to the data result and not the actual result itself.  I don't know if that is true but for certain I cannot pass the data in an array either made as JSON or as a single JavaScript array.</p>
  
  <p>I have tried both ways and the data is always empty on the server side <em>except</em> when I use the test data which is hard coded and not the result of an ajax request.  I think I need to approach this problem in a whole other way, probably using curl on the server PHP side.</p>
  
  <pre><code>        var testData = new Array();
  var record1 = {"var1":"9","var2":"16","var3":"16"};
  var record2 = {"var4":"8","var5":"15","var6":"15"};
  testData.push(record1);
  testData.push(record2);
  
          // send data to server to update records
  $.ajax({
      type: 'POST',
      url: 'statusUpdate.php',                      
      //dataType: 'json',
      data: {'data' : testData},
      //ContentType: 'application/json',
      success: function(data) {
        console.log(data);
      }
  });
  
  RESPONSE
  array(2) {
    [0]=&gt;
    array(3) {
      ["var1"]=&gt;
      string(1) "9"
      ["var2"]=&gt;
      string(2) "16"
      ["var3"]=&gt;
      string(2) "16"
    }
    [1]=&gt;
    array(3) {
      ["var4"]=&gt;
      string(1) "8"
      ["var5"]=&gt;
      string(2) "15"
      ["var6"]=&gt;
      string(2) "15"
    }
  }
  
  
  </code></pre>