2017-07-18 62 views
0

我一直在看這個問題的不少問題,我仍然遇到問題。使用數組中的值填充HTML下拉框

下面是代碼:

<body> 
 

 
<?php 
 
//To start, there is a form with a dropbox and a button, which will have the various functions that can happe 
 
?> 
 

 
<form action="search_start.php" method="post"> 
 
    Function <select name="Functions"> 
 
    <?php 
 
    $fns_to_choose = array(
 
     "Option 1" =>"Get A Monthly Report", 
 
     "Option 2" => "Track One Ship Over Time", 
 
     "Option 3" => "Track Multiple Ships Over Time", 
 
     "Option 4" => "Get Ships Over Time For One$ 
 
    ?> 
 
    <select> 
 
     <?php 
 
     foreach ($fns_to_choose as $key => $value) { 
 
      echo '<option value="' . $key . '">' . $value . '</option>'; 
 
     } 
 
     ?> 
 
    </select> 
 
</form>

什麼情況是,代替使用的數組的元素來填充下拉列表中,所述元件只是「呼應」空下拉下方,它看起來像這樣:

從什麼我從本網站的其他答案中看到,選項應該填充表單,而不僅僅是在其下面。

任何建議絕對歡迎。

我覺得這就像是最基本的東西,我無法弄清楚。

回答

0

有幾個語法錯誤,修正如下,你可以再試一次嗎?

<body> 
 

 

 
<?php 
 

 
//To start, there is a form with a dropbox and a button, which will have the various functions that can happe 
 

 
?> 
 

 
<form action="search_start.php" method="post"> 
 
<!-- here --> 
 

 
<?php 
 

 
$fns_to_choose = array("Option 1" =>"Get A Monthly Report","Option 2" => "Track One Ship Over Time", "Option 3" => "Track Multiple Ships Over Time", "Option 4" => "Get Ships Over Time For One") // here 
 

 
?> 
 

 
<select name="Functions"> <!-- here --> 
 
     <?php 
 
       foreach ($fns_to_choose as $key => $value) 
 
       { 
 
         echo '<option value="' . $key . '">' . $value . '</option>'; 
 
       } 
 
     ?> 
 
</select> 
 
</form> 
 
</body>

0

你有導致嵌套,這是不允許的2個<select>標籤。刪除其中一個,這應該可以解決問題。

+0

事實上,我起身,走過來,回來,看到了問題。謝謝。 –

0

所以,

我發佈後,我找到了答案。問題是我嘗試有效地創建兩個下拉菜單。所以我刪除了第一個,現在它工作得很好。

我把這個留在這裏是因爲..坦率地說,我的愚蠢可能會幫助其他人。