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!
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.
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!
You need to echo the Field
from $col
, Try this:
<option value=""><?= $col['Field'] ?></option>