title: Populate a text box based on a dynamic drop down box in php
body: <p>I am trying to input a value (Rating) from the database when an item is selected. I need to also be able to update that rating and then put that value into another table. 
  My problem is that I'm trying to figure out how to make the value in the text box be related to the selection made in the drop down. I've found coding to make another option box but cannot figure out how to make that a text box.</p>
  
  <p>REWORDING: Originally when I posted the question I thought it was possible to do this without JQuery and was trying to copy the for loop and run a while loop to populate the text box. I now know that isn't possible and would like to figure out how to run a script to populate a text box on change for the drop down box. The problem being the function has to be connected to the database as well as the drop down.
  1. pull the data for the drop down (from the JOURNAL table). 2. Select a journal from that drop down 3. Inside the JOURNAL table a JournalRating has been assigned to every journal pull that value and place inside a textbox.</p>
  
  <p>What I've tried to do is</p>
  
  <pre><code>&lt;?php
      include 'dbc.php';
      connect();
  
      //insert form values into database                                                                                                                                                   
  $sql = "SELECT JournalName, JournalID, Rating, JournalActive from JOURNAL where JournalActive = 1;";      
  //Can take out JournalActive if we do not want it                                                                                                                        
  $result = mysqli_query($conn, $sql);                                                                                                                                                 
  if (!$result) {                                                                                                                                                                      
      $message  = 'Invalid query: ' . mysql_error() . "\n";                                                                                                                            
      $message .= 'Whole query: ' . $query;                                                                                                                                            
      die($message);                                                                                                                                                                   
      echo "there was an issue";                                                                                                                                                       
  } 
  
  $sql2 = "SELECT FName, LName, FacultyID from FACULTY where FacultyActive = 1;"; 
  //Can take out JournalActive if we do not want it                                                                                                                        
  $result2 = mysqli_query($conn, $sql2);                                                                                                                                                 
  if (!$result2) {                                                                                                                                                                      
      $message  = 'Invalid query: ' . mysql_error() . "\n";                                                                                                                            
      $message .= 'Whole query: ' . $query;                                                                                                                                            
      die($message);                                                                                                                                                                   
      echo "there was an issue";                                                                                                                                                       
  } 
  
      //array to hold all of the data                                                                                                                                                      
  $journals = array();                                                                                                                                                                  
      //print out all of the first names in the database   
  $rownumber = 0;                                                                                                                                 
  while ($row = mysqli_fetch_assoc($result)) {                                                                                                                                         
      $journals[$rownumber][0] = $row['JournalName'];
      $journals[$rownumber][1] = $row['JournalID'];
      $journals[$rownumber][2] = $row['JournalRating'];
      $journals[$rownumber][3] = $row['JournalActive'];
      $rownumber++;                                                                                                                                                       
  }
  
  $faculty = array();                                                                                                                                                                  
      //print out all of the first names in the database   
  $rownum = 0;                                                                                                                                 
  while ($row = mysqli_fetch_assoc($result2)) {                                                                                                                                         
      $faculty[$rownum][0] = $row['FName'];
      $faculty[$rownum][1] = $row['LName'];
      $faculty[$rownum][2] = $row['FacultyID'];
      $rownum++;                                                                                                                                                       
  }
  
  ?&gt;
  
  &lt;!DOCTYPE html&gt;
  
  &lt;head&gt;
  &lt;link href="styles.css" rel="stylesheet"&gt;
  &lt;h1&gt; Miami University &lt;/h1&gt;
  &lt;h4&gt; Information Systems and Analytics Department &lt;/h4&gt;
  &lt;script&gt;
  (function($){
      $(function(){ 
          $("#JournalID").on('change', function() {
                $("#JournalRating").val($(this).find("option:selected").data('Rating'));
          });
      }); 
  })(jQuery);
  &lt;/script&gt;
  
  &lt;/head&gt;
  &lt;body&gt;
  
  &lt;div class="StyleDiv" &gt; 
  &lt;!-- coding for journal --&gt;
  &lt;form id="form1" name="form1" method="post" action="RR2.php"&gt;
  
  
  &lt;label for="FacultyID"&gt;Faculty Name&lt;/label&gt;
  &lt;select multiple="multiple" name="FacultyID[]" id="FacultyID"&gt;
  &lt;?php
      for($i = 0; $i &lt; sizeof($faculty); $i++) {
      print "&lt;option value=\"" . $faculty[$i][2] . "\"&gt;" . $faculty[$i][0] .' '. $faculty[$i][1] . "&lt;/option&gt;\r\n";
      }
  ?&gt;
  &lt;/select&gt;
  
  
  &lt;br class="clear" /&gt;
  
  &lt;br class="clear" /&gt; 
  &lt;label for="JournalID"&gt;Journal Name&lt;/label&gt;
  &lt;select name="JournalID" id="JournalID"&gt;
  &lt;?php
  
      for($i = 0; $i &lt; sizeof($journals); $i++) {
      print "&lt;option value=\"" . $journals[$i][1] . "\" data-rating=\"" . $journals[$i][2] . "\"&gt;" . $journals[$i][0] . "&lt;/option&gt;\r\n";
  }
  ?&gt;
  &lt;/select&gt;
  &lt;br class="clear"/&gt;
  
  &lt;label for="JournalRating"&gt;Journal Rating&lt;/label&gt;&lt;input type="text" name="JournalRating" id="JournalRating" /&gt;
  
  &lt;br class="clear" /&gt; 
  
  
  &lt;!-- coding for publication --&gt; 
  &lt;label for="Title"&gt;Publication Title&lt;/label&gt;&lt;input type="text" name="PubID" id="PubID" /&gt;
  &lt;br class="clear" /&gt; 
  &lt;label for="Year"&gt;Year&lt;/label&gt;&lt;input type="text" name="Year" id="Year" /&gt;
  &lt;br class="clear" /&gt; 
  &lt;label for="Volume"&gt;Volume&lt;/label&gt;&lt;input type="text" name="Volume" id="Volume" /&gt;
  &lt;br class="clear" /&gt; 
  &lt;label for="Issue"&gt;Issue&lt;/label&gt;&lt;input type="text" name="Issue" id="Issue" /&gt;
  &lt;br class="clear" /&gt; 
  &lt;label for="Comments"&gt;Comments&lt;/label&gt;&lt;textarea name="Comments" id="Comments" cols="45" rows="5"&gt;&lt;/textarea&gt;
  &lt;br class="clear" /&gt; 
  
  
  
  &lt;input type="submit" name="Submit" id="Submit" value="Submit" /&gt;
  &lt;br class="clear" /&gt; 
  &lt;/br&gt;
  &lt;/br&gt;
  
  &lt;/div&gt;
  &lt;/form&gt;
  
  &lt;?php
  
  //Post Parameters 
  $JournalID = $_POST['JournalID'];
  //for($i = 0; $i &lt; sizeof($journals); $i++) {
      //if ($JournalID = $journals[$i][1]) {
      //$JournalName = $journals[$i][0];
      //}
      //}
  $Year = $_POST['Year'];  
  $Comments = $_POST['Comments'];  
  $Volume = $_POST['Volume'];  
  $Issue = $_POST['Issue'];  
  $Title = $_POST['Title'];
  $JournalRating = $_POST['JournalRating'];
  $FacultyMemID = $_POST['FacultyID'];
  
  
  //Query 
  
  
  
   //INSERT 
  $stmt = $conn-&gt;prepare(" INSERT INTO PUBLICATION ( JournalID, Year, Comments, Volume, Issue, Title, JournalRating )  VALUES ( ?, ?, ?, ?, ?, ?, ? )"); 
  $stmt-&gt;bind_param('sssssss', $JournalID, $Year, $Comments, $Volume, $Issue, $Title, $JournalRating);
  $stmt-&gt;execute(); 
  
  $pubID = $stmt-&gt;insert_id;
  $facmemid = 0; 
  
  $stmt = $conn-&gt;prepare(" INSERT INTO FACULTYPUBLICATIONS ( FacultyID, PubID )  VALUES ( ?, ? )"); 
  $stmt-&gt;bind_param('ii', $facmemid, $pubID);
   //for ($_POST['FacultyID'] as $FacultyMemID) {
   for($i = 0; $i &lt; sizeof($FacultyMemID); $i++) {
      $facmemid = $FacultyMemID[$i]; 
      $stmt-&gt;execute();
      }
  
  mysqli_close($conn);
  ?&gt;
  
  
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>