title: How to use a foreach loop with AJAX call?
body: <p>I have a foreach loop that is called from my AJAX code. It appears that the foreach loop is being skipped right over. I would ultimately like to make it so that the foreach loop will execute a query using each value in the array, but I am first trying to test the the foreach loop works (which it does not). The alert (from my AJAX) that I am receiving is just the original array and not the one with the added items.</p>
  
  <p>PHP:</p>
  
  <pre><code>   $data = array();
     if(isset($_POST['myArray']) &amp;&amp; !empty($_SERVER['HTTP_X_REQUESTED_WITH']){ 
      $data = $_POST['myArray']; 
      foreach ($data as $item) {
          $data[] = $item;
      }
      echo json_encode($data);
      die();      
  } 
  </code></pre>
  
  <p>AJAX:</p>
  
  <pre><code>$(document).ready(function(){
      $('#btn-addkegs').click(function() {
          var arr = kegs;
          var myArray =  JSON.stringify(arr);
  
          $.ajax({
              url: "addkegs.php",
              method: "POST",
              dataType: "json",
              data: {myArray: myArray},
              success: function (result) {
              alert("Your kegs have been added! These include: " + result);
              textarea.value = "";
              kegs = [];
           }
                      });
  
              });
          });
  </code></pre>
  
  <p>As for the line <code>var arr = kegs;</code>, the value of the array 'kegs' is set through an input field and other AJAX, but all that works fine. I think that my problem is with my PHP code.</p>