I have a HTML form that has a selection drop down. I would like to populate the drop down from my MySQL DB. The HTML form already has some java scripts to handle datepicker, and current date on one of the fields in the form.
I can code the form for the selection manually, but would like it to come from MySQL. the php I am using I put in it's own file name.php to make sure it worked correctly. I added some echo statements in the php to build the form, and the selection, and then populate the selection, and close it. this worked just fine as a standalone php.
When I append the php in the form under the selection definition, it does not populate the selection at all, it is left blank/empty. If I add some echo statments to output info in the format of a selection option, the first echo never shows up, and the subsequent echos show up, but any variable show up as the variable name.
<form id="form1" name="form1" method="post" action="/db_log.php">
Field:<select name="id">
<?php
require_once dirname(__FILE__) . '/db_connect.php';
$db = new DB_CONNECT();
$query="SELECT id, name FROM file order by name";
$result=mysql_query($query);
while ($row = mysql_fetch_array($result)) {
$id = $row['id'];
$name =$row['name'];
echo "<option value=\"$id\"> $name </option>";
}
?>
</select><br />
</form>
the above yealds an empty select dropdown. But this works great as a PHP file.
<?php
echo "<form id=\"form1\" name=\"form1\" method=\"post\" action=\"/db_log.php\">";
echo "Select:<select name=\"id\">";
require_once dirname(__FILE__) . '/db_connect.php';
$db = new DB_CONNECT();
$result=mysql_query("SELECT id, name FROM file order by name");
while ($row = mysql_fetch_array($result)) {
$id = $row['id'];
$name =$row['name'];
echo "<option value=\"$id\"> $name </option>";
}
echo "</select><br />";
echo "</form>";
?>
If I add a echo to the HTML form one like a test echo before any other echo, it is ignored.
<form id="form1" name="form1" method="post" action="/db_log.php">
Field:<select name="id">
<?php
echo "<option value=\"1\"> Test-name </option>";
require_once dirname(__FILE__) . '/db_connect.php';
$db = new DB_CONNECT();
$query="SELECT id, name FROM file order by name";
$result=mysql_query($query);
while ($row = mysql_fetch_array($result)) {
$id = $row['id'];
$name =$row['name'];
echo "<option value=\"$id\"> $name </option>";
}
?>
</select><br />
</form>
the only thing that shows up from this is the option to select $name