2017-07-18 82 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

所以,

我发布后,我找到了答案。问题是我尝试有效地创建两个下拉菜单。所以我删除了第一个,现在它工作得很好。

我把这个留在这里是因为..坦率地说,我的愚蠢可能会帮助其他人。