douao2000
2016-04-13 10:02
浏览 90
已采纳

将php变量添加到下拉菜单中(在Flask中)

I originally had this script to populate a dropdown menu with query results:

<?php
require_once("dbcontroller.php");
$db_handle = new DBController();
if(!empty($_POST["data_id"])) {
    $query ="SHOW COLUMNS FROM table_name";
    $results = $db_handle->runQuery($query);
?>
    <option value="">Select Parameter</option>
<?php
    foreach($results as $col) {
?>
    <option value=""><?= $col ?></option>
<?php
    }
}

However the entry was always empty. I checked if this was a php syntax problem using both <?= and <?php echo as suggested in Populating dropdown list

No luck. So next I checked if there actually is something in my query result so I simple replaced the dropdown entry with a string variable:

<?php
$varName= 'testingList';
?>
    <option value=""><?= $varName ?></option>

Again I tried both syntax options but still no luck (i.e. testingList does not show up in the dropdown). Instead, I get a new blank dropdown option i.e. 1 empty entry is added. (PS. the value assigned will also be the same variable but I'm leaving it out at the moment to make sure its not an issue - so just focusing on getting a variable name in the dropdown menu).

Currently running PHP 5.5.9 on a remote server; the issue is also local. Any ideas greatly appreciated. Thanks!

UPDATE

When I run the .php file independently, works just fine. But not in the flask environment so I thought I'll add the fact that I'm working in Flask.

图片转代码服务由CSDN问答提供 功能建议

我最初使用此脚本填充包含查询结果的下拉菜单:

 &lt;?php 
require_once(“dbcontroller.php”); 
 $ db_handle = new DBController(); 
if(!empty($ _ POST [“data_id”])){
 $ query =“ 从table_name显示列“; 
 $ results = $ db_handle-&gt; runQuery($ query); 
?&gt; 
&lt; option value =”“&gt;选择参数&lt; / option&gt; 
&lt;?php \  n foreach($结果为$ col){
?&gt; 
&lt; option value =“”&gt;&lt;?= $ col?&gt;&lt; / option&gt; 
&lt;?php 
} 
  } 
   
 
 

但是条目始终为空。 我根据&lt;?= &lt;?php echo 检查了这是否是一个php语法问题 / questions / 23546818 / php-foreach-loop-to-populate-dropdown-list-with-values-from-an-array-of-arrays“>填充下拉列表 < p>没有运气。 接下来我检查了我的查询结果中是否确实存在某些内容,所以我简单地将下拉条目替换为字符串变量:

 &lt;?php 
 $ varName ='testingList  '; 
?&gt; 
&lt; option value =“”&gt;&lt;?= $ varName?&gt;&lt; / option&gt; 
   
 
 

再次 我尝试了两种语法选项,但仍然没有运气(即,testList没有显示在下拉列表中)。 相反,我得到一个新的空白下拉选项,即添加1个空条目。 (PS。分配的值也将是相同的变量,但我现在要将其保留,以确保它不是问题 - 所以只关注在下拉菜单中获取变量名称。)

目前在远程服务器上运行PHP 5.5.9; 这个问题也是本地的。 任何想法都非常感激。 谢谢!

UPDATE

当我独立运行.php文件时,工作正常。 但不是在烧瓶环境中,所以我想我会添加我在Flask工作的事实。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doujishao8793 2016-04-13 16:29
    已采纳

    I have fixed this by switching everything to flask. Since this was a multiple dropdown menu, i.e. the value selected in the first dropdown populates the second dropdown, I sent the selected value from the first dropdown back to flask through Ajax, and populated the second dropdown using flask!

    打赏 评论
  • duanbixia7738 2016-04-13 10:10

    You need to echo the Field from $col, Try this:

    <option value=""><?= $col['Field'] ?></option>
    
    打赏 评论

相关推荐 更多相似问题