title: Loop through $_POST array isn't working
body: <p>I have a php function that is supposed to loop through an array of $_POST data containing record ids, but for the life of me I can't figure out why it isn't working. I have similar functionality elsewhere in my site, but this particular one want to be difficult:</p>
  
  <pre><code>if (isset($_POST['classNumberAdd']) &amp;&amp; isset($_POST['classNameAdd'])) {
      $classNumber = $_POST['classNumberAdd'];
      $className = $_POST['classNameAdd'];
  
      $sqlAddClass = "INSERT INTO classes (className, classNumber) VALUES ('" . $className . "', '" . $classNumber . "')";
  
      include('../includes/dbConnect.php');
  
      $addClassResult = @mysqli_query($dbc, $sqlAddClass);
  
      // Get the id of the class just inserted            
      $sqlGetClassID = "SELECT classID FROM classes WHERE className = '" . $className . "' AND classNumber = '" . $classNumber . "'";
      $getClassIdResult = @mysqli_query($dbc, $sqlGetClassID);
      $classID = 0;
  
      while ($row = mysqli_fetch_row($getClassIdResult)) {$classID = $row[0];}
  
      $log-&gt;lwrite('Before check if array set: docsToAddToClass array: ' . $_POST['docsToAddToClass']);
  
      // Add any checked docs to the new class
      if (isset($_POST['docsToAddToClass'])) {
  
          $log-&gt;lwrite('After check if array set and before addDocs function call: docsToAddToClass array: ' . $_POST['docsToAddToClass']);
  
          foreach($_POST['docsToAddToClass'] as $docID) {
              $log-&gt;lwrite('Doc id being added to new class: ' . $docID);
              // Add each class to the document
              $sqlAddClassToDoc = "INSERT INTO classDocs (classDocsClassID, classDocsDocID) VALUES ('" . $classID . "', '" . $docID . "')";
  
              $resultAddDocToClass = @mysqli_query($dbc, $sqlAddClassToDoc);
          }
      }
  
      mysqli_close($dbc);
      echo $classID;
  }
  </code></pre>
  
  <p>As you can see I have several lines that log where I'm at. I never get to the line "<code>$log-&gt;lwrite('Doc id being added to new class: ' . $docID);</code>", so for some reason I'm not getting in the array. The last line to get written to the log on any run through is the "After check if array set..." line.</p>
  
  <p>Originally, instead of the foreach loop, I was passing the POST and class id to a function (defined as <code>function addDocsToClass($docsArray, $classID)</code>, but that didn't work, so I moved that whole thing into the code as above to troubleshoot.</p>
  
  <p>I've tried setting the $_POST array to a variable first (such as <code>$docsArray = $_POST['docsToAddToClass'])</code>) but nothing I've tried so far works.</p>
  
  <p>What am I missing/doing wrong?</p>
  
  <p><strong>[EDIT]
  New information based on closer look at my code:</strong></p>
  
  <p>I'm calling the above php script in an $.ajax() call, which is passing docsToAddToClass as a comma delimited list of ids retrieved and built by the following lines:</p>
  
  <pre><code>$.each($('#docsTable input[name="docsToAddToClass[]"]:checked'), function() {
      docsToAddToClass.push($(this).val());
  });
  </code></pre>
  
  <p>As mentioned in a reply below, $_POST['docsToAddToClass'] prints out in my log as 35,36,37, etc. A javascript alert() displays the same output, as well. So I decided to use php explode to build an array to iterate through:</p>
  
  <pre><code>$docsArray = explode(',', $_POST['docsToAddToClass']);
  </code></pre>
  
  <p>However, only the first id in the list is getting put into the variable, which ends up NOT being an array, breaking the script.</p>
  
  <p>So, even after discovering all of that, I'm pretty much back at square one!</p>
  
  <p>How do I iterate through the list of ids in $_POST['docsToAddToClass'], when it is formattted as 35,36,37, and get each id as if $_POST were a regular php array?</p>