简体   繁体   中英

Drop Down Option not being selected

I have the following code that builds two drop down menus that are populated by values from a database. The first drop down menu prints the user selection but the second doesn't. How can I fix this? PS I do not want to use AJAX

 //first drop down
 echo<<<FORMSTART
<form name= "modules" method= "post">
<select name = "modules" onChange="document.topic_list.submit()">
<option value = "None">Choose module</option>
FORMSTART;

$stmt = $pdo->query("SELECT DISTINCT Module from timetable");

 //populate drop down menu
 while ($row = $stmt->fetch()){

    echo "<option value= '" . $row['Moudle'] . "'>". $row['Module'] . "</option>";
 }

 //End of first form
 echo<<<FORMEND
 </select>

  </form>
  FORMEND;

 $selected_module = $_POST["Module"]; 
 echo "$selected_module Selected" ; 

  //second drop down
  echo<<<FORMSTART
  <form name= "time_list" method= "post">
 <select name = "Time selected:">
 <option value = "None">Select a Time</option>
 FORMSTART;

   $stmt = $pdo->query("SELECT Times FROM Timetable WHERE Module='" . $selected_module. "' AND capacity != 0"); 

 //populate drop down menu
 while ($row = $stmt->fetch()){
    echo "<option value= '" . $row['Times'] . "'>". $row['Times'] . "</option>";
 }

 //End form for second drop down which wont print

  $selected_time = $_POST["time_list"]; 
 echo $selected_time;  //this wont print, im guessing because it isnt stored
 echo<<<FORMEND
 </select>
 </form>
 FORMEND;
             //first drop down
         echo<<<FORMSTART
         <form name= "modules" method= "post">
         <select name = "modules" onChange="document.topic_list.submit()">
         <option value = "None">Choose module</option>
         FORMSTART;

         $stmt = $pdo->query("SELECT DISTINCT Module from timetable");

         //populate drop down menu
         while ($row = $stmt->fetch()){

            echo "<option value= '" . $row['Moudle'] . "'>". $row['Module'] . "</option>";
         }

         //End of first form
         echo<<<FORMEND
         </select>

          </form>
          FORMEND;

         $selected_module = $_POST["Module"]; 
         echo "$selected_module Selected" ; 

          //second drop down
          echo<<<FORMSTART
          <form name= "time_list" method= "post">
         <select name = "time_list">
         <option value = "None">Select a Time</option>
         FORMSTART;

           $stmt = $pdo->query("SELECT Times FROM Timetable WHERE Module='" . $selected_module. "' AND capacity != 0"); 

         //populate drop down menu
         while ($row = $stmt->fetch()){
            echo "<option value= '" . $row['Times'] . "'>". $row['Times'] . "</option>";
         }

         //End form for second drop down which wont print


         echo<<<FORMEND
         </select>
$selected_time = $_POST["time_list"]; 
        echo $selected_time;  /*you need to ensure that you put this after </select> tag. It can also work if you put it after the closing form tag </form>*/
         </form>
         FORMEND;

The major challenge you faced is trying to print in a select tag. If you take a look at the first drop down that prints, your echo statement is after closing the select tag. You can only print or echo in a select tag as an option value or label, and not as a text to be displayed on the page.

Another thing to look at is the name you give here <select name = "Time selected:"> which does not correlate with $_POST["time_list"];

That should solve the problem.

Make that adjustment and enjoy yourself!!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM