title: PHP - multiple forms, get values from both upon submit?
body: <p>I am a little stuck at the moment and could use some help please.</p>
  
  <p>I have a calendar which is being used by multiple departments to enter their schedule in. Each user is asked to complete 1x select and 2x input fields which are divided into FORM 1 and FORM 2</p>
  
  <p>FORM 1:</p>
  
  <ul>
  <li>department code (onclick submit event)</li>
  </ul>
  
  <p>FORM 2:</p>
  
  <ul>
  <li>name</li>
  <li>date</li>
  <li>submit button</li>
  </ul>
  
  <p>After the user has selected his/her department code, the calendar will refresh and fetch the entries for this specific department only (meaning it will filter all records for the department code). After that, the user has to enter his username and select a date, followed by pressing the submit button (form 2).</p>
  
  <p>Now, the problem is that in order for FORM 2 to submit correctly, I need to know the department code from Form 1. Additionally, I am having troubles preventing the calendar from refreshing (Form 1) when pressing the submit button for Form 2.</p>
  
  <p>In short, how can I clearly distinguish the ($_POST) between form 1 and form 2, while accessing both form's data?</p>
  
  <p>PHP</p>
  
  <pre><code>&lt;?php
  // SHOULD BE EXECUTED AFTER THE SUBMIT BUTTON WAS CLICKED
  if ($_SERVER['REQUEST_METHOD'] == 'POST' &amp;&amp; isset($_POST['type']) &amp;&amp; ($_POST['type']=='new_entry')) {
  
      // include connection details
      include '../../plugins/MySQL/connect_db.php';
  
      // Open a new connection to the MySQL server
      $con = new mysqli($dbhost,$dbuser,$dbpass,$dbname);
  
      // Output any connection error
      if ($con-&gt;connect_error) {
          die('Error : ('. $con-&gt;connect_errno .') '. $con-&gt;connect_error);
      }
  
      // define variables
      $table = 'calendar';
      $type = mysqli_real_escape_string($con,$_POST['type']);
      $holidex = mysqli_real_escape_string($con,$_POST['holidex']);
  
      if($type == 'new_entry')
      {               
          // define variables and query
          $mod_property = mysqli_real_escape_string($con,$_POST['holidex']);
          $mod_name = mysqli_real_escape_string($con,$_POST['mod_name']);
          $mod_date = date('Y-m-d',strtotime($_POST['mod_date']));
  
          $sql = "INSERT INTO calendar (`title`, `startdate`, `enddate`, `allDay`, `color`, `holidex`) VALUES ('$mod_name','$mod_date','$mod_date','true','','$mod_property')";
  
          print($sql);
  
          $result = $con-&gt;query($sql) or die('&lt;p&gt;Could not submit new MOD record into database: ' . MYSQLI_ERROR() . '&lt;/p&gt;');
  
          $result-&gt;free();
      }
  
      $con-&gt;close();
  }
  ?&gt;
  
  
  
          &lt;form name="form1" action="&lt;?php echo $_SERVER['PHP_SELF']; ?&gt;" method="POST"&gt;
          &lt;!-- &lt;form name="mod_form" action="../../plugins/MySQL/ajax_action.php?type=new_entry" method="POST"&gt; --&gt;
  
            &lt;!-- Property --&gt;
            &lt;div class="col-md-4"&gt;
              &lt;label&gt;Property&lt;/label&gt;
              &lt;div class="input-group"&gt;
                  &lt;span class="input-group-addon"&gt;&lt;i class="fa fa-server"&gt;&lt;/i&gt;&lt;/span&gt;
                  &lt;select name="holidex" id="holidex" class="form-control select2" style="width: 100%;" data-placeholder="Select your property" onchange="this.form.submit();" method="POST" &lt;?php if($_SESSION['Access']=='User') { echo "disabled"; } ?&gt;&gt;
  
                      // get all my departments and their respective codes
                      &lt;option value="1"&gt;Dept. 1&lt;/option&gt;
                      &lt;option value="2"&gt;Dept. 2&lt;/option&gt;
                      &lt;option value="3"&gt;Dept. 3&lt;/option&gt;
  
                  &lt;/select&gt;
              &lt;/div&gt;
              &lt;!-- /btn-group --&gt;
            &lt;/div&gt;
            &lt;!-- /.property --&gt;
  
          &lt;/form&gt;
  
          &lt;form name="form2" action="&lt;?php echo $_SERVER['PHP_SELF']; ?&gt;" method="POST"&gt;
            &lt;!-- MOD Name --&gt;
            &lt;div class="col-md-4"&gt;
              &lt;label&gt;MOD Name&lt;/label&gt;
              &lt;div class="input-group"&gt;
                  &lt;span class="input-group-addon"&gt;&lt;i class="fa fa-user"&gt;&lt;/i&gt;&lt;/span&gt;
                  &lt;select name="mod_name" id="mod_name" class="form-control select2" style="width: 100%;" data-placeholder="Select a name"&gt;
                      &lt;option value=""&gt;&lt;/option&gt;
                      &lt;option value="1"&gt;User1&lt;/option&gt;
                      &lt;option value="2"&gt;User2&lt;/option&gt;
                      &lt;option value="3"&gt;User3&lt;/option&gt;
                  &lt;/select&gt;
              &lt;/div&gt;
              &lt;!-- /.input-group --&gt;
            &lt;/div&gt;
            &lt;!-- /.mod name --&gt;
  
            &lt;!-- MOD Date --&gt;
            &lt;div class="col-md-3"&gt;
              &lt;label&gt;MOD Date&lt;/label&gt;
              &lt;div class="input-group"&gt;
                &lt;span class="input-group-addon"&gt;&lt;i class="fa fa-server"&gt;&lt;/i&gt;&lt;/span&gt;
                &lt;input type="date" class="form-control" name="daterangepicker" id="daterangepicker" /&gt;
                &lt;!-- &lt;input type="date" id="mod_date" name="mod_date" class="form-control" style="width: 100%;"&gt; --&gt;
              &lt;/div&gt;
            &lt;/div&gt;
            &lt;!-- /.mod date --&gt;
  
              // hidden input field to determine the type and help differentiate the $_POST submissions
              &lt;input type="hidden" class="form-control" name="type" id="type" value="new_entry"/&gt;
  
            &lt;!-- Submit button --&gt;
            &lt;div class="col-md-1 text-center"&gt;
              &lt;label&gt;&amp;nbsp;&lt;/label&gt;
              &lt;button type="submit" name="btnSubmit" class="btn btn-primary btn-block" onclick="this.disabled=true; this.value = 'Wait...'; this.form.submit(); return true;"&gt;Submit&lt;/button&gt;
            &lt;/div&gt;
            &lt;!-- /.submit button --&gt;
  
          &lt;/form&gt;
          &lt;!-- /.form 2 --&gt;
  </code></pre>