title: Updating table based on dropdown list selected value via PHP mysql PDO
body: <p>I cannot figure this out at all. I am populating my table by getting data from the database and filling up both the dropdown and table. I need to get the dropdown value and once that value is selected, I need to update the table via a different mysql command that gets the data. I am unable to figure out how to get the dropdown list value to change the mysql query and update the table. My code is below.</p>
  
  <pre><code>&lt;?php
  session_start();
  include '/Header.php';
  include '/Footer.php';
  include '/Functions.php';
  extract($_POST); 
  
  $dbConnection = parse_ini_file("/db_connection.ini");
  extract($dbConnection);
  $myPdo = new PDO($dsn, $user, $password);
  $myPdo-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  
  function fill_drp($myPdo) {
      $output = '';
      $query = $myPdo-&gt;prepare("SELECT Term, Year FROM Semester");
      $query-&gt;execute();
  
      foreach ($query-&gt;fetchall(PDO::FETCH_ASSOC) as $row) {
         $output .= '&lt;option value="' . $row['Year'] . ' ' . $row['Term'] . '"&gt;' . $row['Year'] . ' ' . $row['Term'] . '&lt;/option&gt;';
  
          #$year = substr(($row['Year'] . $row['Term']), 2, 3); #need it to be 18W instead to query database
      }
      return $output;
  }
  
  function fill_table($myPdo, $selected_term) {
      $output = '';
      $query = $myPdo-&gt;prepare("select CourseOffer.CourseCode, 
                              Title, WeeklyHours from CourseOffer join Course 
                              on CourseOffer.CourseCode = Course.CourseCode 
                              where SemesterCode= '" . $selected_term . "'");
      $query-&gt;execute();
      foreach ($query-&gt;fetchall(PDO::FETCH_ASSOC) as $row) {
          $cc = $row['CourseCode'];
          $output.= "&lt;tr&gt;";
          $output.= "&lt;td&gt;" . $row['CourseCode'] . "&lt;/td&gt;";
          $output.= "&lt;td&gt;" . $row['Title'] . "&lt;/td&gt;";
          $output.= "&lt;td&gt; " . $row['WeeklyHours'] . "&lt;/td&gt;";
          $output.="&lt;td&gt; &lt;input type='checkbox' name='chk[]' value='$cc'" . "&lt;/td&gt;";
          $output.= "&lt;/tr&gt;";
      }
      return $output;
  }
  ?&gt;
  
  
  &lt;html&gt;
      &lt;head&gt;
          &lt;title&gt;Online Course Registration&lt;/title&gt;
          &lt;meta charset="utf-8" name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"&gt;
          &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"&gt;&lt;/script&gt;  
      &lt;/head&gt;
      &lt;body&gt;
          &lt;h1 class="text-center"&gt;Course Selection&lt;/h1&gt;
          &lt;p&gt;Welcome &lt;strong&gt;&lt;?php echo $_SESSION['name']; ?&gt;&lt;/strong&gt; (not you? change user &lt;a href="Login.php"&gt;here&lt;/a&gt;)&lt;/p&gt;
          &lt;p&gt;You have registered &lt;strong&gt;#todo&lt;/strong&gt; hours of course(s) for the semester&lt;/p&gt;
          &lt;p&gt;You can register &lt;strong&gt;#todo&lt;/strong&gt; more hours of course(s) for the semester&lt;/p&gt;
          &lt;p&gt;Please note that the courses you have registered will not be displayed in the list&lt;/p&gt;
  
  
          &lt;form action="CourseSelection.php" method="post"&gt;
              &lt;br/&gt;
              &lt;div class="dropdown text-right"&gt;
                  &lt;select class="dropdown" id="terms" name="terms" &gt;
                      &lt;?php
                      echo fill_drp($myPdo);
                      ?&gt;
                  &lt;/select&gt; 
              &lt;/div&gt; 
              &lt;div id="tableContainer"&gt; 
                  &lt;table border="1" class="table" id="table1"&gt;
                      &lt;thead class="thead-light"&gt;
                          &lt;tr&gt;
                              &lt;th scope="col" &gt;Code&lt;/th&gt;
                              &lt;th scope="col"&gt; Course Title&lt;/th&gt;
                              &lt;th scope="col"&gt;Hours&lt;/th&gt;
                              &lt;th  scope="col"&gt;Select&lt;/th&gt;
  
                          &lt;/tr&gt;
                          &lt;?php
                          if ($_POST["term"] != null) {
                              $_SESSION['term'] = $_POST["term"];#attempt at getting selected dropdown value.
                          }
                          if (isset( $_SESSION['term'])) {
                              $selected_val = substr(($_SESSION['term']), 2, 3);
                              echo fill_table($myPdo, $selected_val); 
                          } else {
                              echo fill_table($myPdo, '17F');#default is 17F but could be 18W 19S etc..
                          }
                          ?&gt;
                  &lt;/table&gt;
              &lt;/div&gt;
              &lt;br/&gt;
              &lt;input type='submit'  class="btn btn-primary"  class='button' name='submit' value='submit'/&gt;
          &lt;/form&gt;
      &lt;/body&gt;
  
  &lt;/html&gt;
  </code></pre>
  
  <p>Ajax component</p>
  
  <p>I added this script right below my table</p>
  
  <pre><code>   &lt;script&gt;  
          $(document).ready(function(){  
               $('#terms').change(function(){  
                    var term = (($(this).val()).replace(' ','')).substring(2, 5);  
                    //var term = '19W';
                    console.log(term); //shows that it's getting the semesters
                    $.ajax({  
                         url:"RefreshTable.php",  
                         method:"POST",  
                         data:{term:term},  
                         success:function(data){  
                              $('#tableContainer').html(data);  
                         }  
                    });  
               });  
          });  
          &lt;/script&gt; 
  </code></pre>
  
  <p>My refreshtable.php has the content below</p>
  
  <pre><code>&lt;?php
  $dbConnection = parse_ini_file("/db_connection.ini");
  extract($dbConnection);
  $myPdo = new PDO($dsn, $user, $password);
  $myPdo-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  $output = '';
  if (isset($_POST["term"])) {
      if ($_POST["term"] != '') {
          $query = $myPdo-&gt;prepare("select CourseOffer.CourseCode, 
                              Title, WeeklyHours from CourseOffer join Course 
                              on CourseOffer.CourseCode = Course.CourseCode 
                              where SemesterCode= '".$_POST["term"]."'");
      } else {
          $query = $myPdo-&gt;prepare("select CourseOffer.CourseCode, 
                              Title, WeeklyHours from CourseOffer join Course 
                              on CourseOffer.CourseCode = Course.CourseCode 
                              where SemesterCode= '17F'");
      }
      $query-&gt;execute();
      foreach ($query-&gt;fetchall(PDO::FETCH_ASSOC) as $row) {
          $cc = $row['CourseCode'];
          $output.= "&lt;tr&gt;";
          $output.= "&lt;td&gt;" . $row['CourseCode'] . "&lt;/td&gt;";
          $output.= "&lt;td&gt;" . $row['Title'] . "&lt;/td&gt;";
          $output.= "&lt;td&gt; " . $row['WeeklyHours'] . "&lt;/td&gt;";
          $output.="&lt;td&gt; &lt;input type='checkbox' name='chk[]' value='" . $row['CourseCode'] . "'" . "&lt;/td&gt;";
          #(isset($copies) ? $copies[$i] : '') . "' &gt;&lt;/td&gt;";
          $output.= "&lt;/tr&gt;";
      }
      echo $output;
  }
  ?&gt;
  </code></pre>