title: PHP script only posting to db after first submission
body: <p>I built my own custom CMS back end to one of the websites I have been working and have been experiencing some strange behavior with the functionality of my site.</p>
  
  <p>The admin is first presented with a form that has two sections to fill out "Banner" and "Section 1", he/she is then able to click a button off to the side of the screen to append new sections to the page, this is handled via some jQuery to append the new element to the document.</p>
  
  <p>The script till here behaves as usual, all sections are able to be filled out, but on submission a tonne of errors are thrown by the script.  If the info is inputted again, the form submits as expected and all the correct information is submitted to the database. What I want to know is, is jQuery conflicting with the way my DOM is behaving upon submission or have I made a mistake in my PHP script? I have been stuck with this the past few days, any suggestions would be much appreciated.</p>
  
  <p>Here we check which view to render</p>
  
  <pre><code>    &lt;?php if(!isset($_POST['submission'])){
          include_once("includes/getsection_view.php");
      } else if(isset($_POST['submission'])){
          include_once("includes/getsection_view_return.php");
      }
  ?&gt;
  </code></pre>
  
  <p>Markup </p>
  
  <p>getsection_view.php (default view)</p>
  
  <pre><code>&lt;form method="POST" enctype="multipart/form-data" id="theForm"&gt;
      &lt;?php 
          $title = "";
          $body = "";
  
          if(isset($_POST['submit'])){
               for($i=0; $i &lt;=count($nodes); $i++)
               {
                          if(isset($_POST['sectionTitle'.$i])){ $title+$i = htmlentities($_POST['sectionTitle'.$i]);};
                          if(isset($_POST['sectionContent'.$i])){ $body+$i = htmlentities($_POST['sectionContent'.$i]);};
                  }
              }
      ?&gt;
          &lt;div id="sectionInfo"&gt;
              &lt;div id="sectionWrap"&gt;
                  &lt;div id="sectionInner"&gt;
                      &lt;label class="inst head"&gt;Section 1&lt;/label&gt;
                      &lt;input class="textOver" type="text" name="sectionTitle1" value="&lt;?php echo $title; ?&gt;" placeholder="Section Title" onfocus="this.select();"&gt;
                      &lt;label class="inst ib"&gt;Please enter any text you would like associated with the section.&lt;/label&gt;
                      &lt;textarea style="margin-top: 3px" name="sectionContent1" value="&lt;?php echo $body; ?&gt;" onfocus="this.select();" placeholder="Section Description"&gt;&lt;/textarea&gt;
                      &lt;label class="inst ib"&gt;Please upload the image associated with this section, .PNG required (588x514px).&lt;/label&gt;
                      &lt;input type="file" style="visibility:hidden;" name="sectionImg1" class="upload" /&gt;
                      &lt;input type="button" id="fileStyle" class="fSOver fileStyle" value="Upload Section Image!" /&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;          
          &lt;br&gt;
          &lt;div align="center" class="logBut" id="postBut"&gt;
              &lt;input style="width: 942px !important; margin-left: -40px !important" type="submit" name="submission" value="Add pump sections to the database" /&gt;
          &lt;/div&gt;
      &lt;/form&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Finally upon submission this script runs to input the data into the database</p>
  
  <pre><code>  # Get the total section node count (not banner)
       $nodes = 0;
       foreach($_POST as $key =&gt; $section){ 
      $nodes++;
       }
  
      // Get the correct total of apended nodes
       $nodes = (($nodes-3)/2)-1; 
       $i = 1;
  
  
  if(isset($_POST['submission'])){
  
  // Check for errors on the banner section
  if(empty($errors) === true){
  
      // Perform any last checks
      if(isset($_POST['ptype'])){$serial = $_POST['ptype'];};
  
      // Banner section validation and input was here...
  
      // Handle each section
      //*********************
      $sSectionId = 1;
  
      // Validate each section input
      for($i=1; $i&lt;$nodes+1; $i++){
          if(empty($_POST['sectionTitle'.$i]) || empty($_POST['sectionContent'.$i])){
              $errors[] = 'Please fill in all fields at Section ' .$i;
          }   
  
          // Image validation
          if (!isset($_FILES['sectionImg'.$i])) {
              $errors[] = 'Please upload a file in PNG format at Section'.$i.'.';
          } else if (isset($_FILES['sectionImg'.$i])) {
              $fileName = $_FILES['sectionImg'.$i]['name'];
  
                  $target_path = IMG_DIR . basename($_FILES['sectionImg'.$i]['name']);
  
                  if(move_uploaded_file($_FILES['sectionImg'.$i]['tmp_name'], $target_path)) {
  
                  } else {
                      $errors[] = "There was an error whilst uploading " . $fileName . " please try again.";
                  }
  
          }
  
          // Initiate variables for PDO insert
          $sImg = "/" . $fileName;
          $sHeader = $_POST['sectionTitle'.$i];
          $sInfo = $_POST['sectionContent'.$i];
  
          // If the errors are 100% empty then input the new section to DB
          if(empty($errors)===true){
              $users-&gt;add_section($sHeader, $sInfo, $sImg, $serial, $sSectionId);
              $success[] = $nodes+1 . " sections have been added to " . $serial . ".";
          }
  
          else {
              $errors[] = "It seems something went wrong when trying to add your data to the system, please try again.";
          }
  
          $sSectionId += 1;   
      }
  
  }
  }
  </code></pre>
  
  <p>As I stated earlier, the code here works, but only once getsection_view_return.php is called, it does not work with the getsection_view.php script, if anyone has any ideas as to why I am experiencing this behavior I would be most grateful of any pointers.</p>
  
  <p>If anymore code information is required please request and I will include it in an edit.</p>
  
  <p>NOTE: If anybody needs the full markup please check edit log, this is minified.</p>