下拉选项未被选中

时间:2018-04-07 22:03:18

标签: php html mysql pdo

我有以下代码构建两个下拉菜单,这些菜单由数据库中的值填充。第一个下拉菜单打印用户选择,但第二个下拉菜单不打印。我怎样才能解决这个问题? P.S我不想使用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;

1 个答案:

答案 0 :(得分:0)

             //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;

您面临的主要挑战是尝试在选择标签中打印。如果你看一下打印的第一个下拉列表,你的echo语句就是在关闭select标签之后。 您只能在选择标记中打印或回显选项值或标签,而不能作为要在页面上显示的文本。

要查看的另一件事是您在此处提供的名称<select name = "Time selected:">$_POST["time_list"];

无关

那应该可以解决问题。

进行调整并尽情享受!!!