Jump to content

printing values to a drop down box


woodplease

Recommended Posts

i'm trying to print out values from a table into a drop down box, but my code doesnt seem to work. when i test it, there are no values in the drop down box.

 

if someone could find the problem, that would be great.

<form name="add_sub_section" method="post" action="<?php echo $_SERVER['../PHP_SELF']; ?>">
Sub Section Name <input type="text" name="sub_section_title" maxlength="200" /><br/><br/>
Sub Section desc <textarea name="sub_section_desc" rows="4" columns="30" /></textarea><br/><br/>
SECTION 
<?php
$list = "SELECT section_title FROM section_main";
echo "<select name='section_title'>";
while ($a = mysql_fetch_row($list)) {
for ($j = 0; $j < mysql_num_fields($list); $j++) {
echo "<option value=". $a[$j] . ">". $a[$j] . "</option>";
}
}
echo "</select>";
?>
<br/><br/>
<input type="submit" name="submit" value="Add Section"/>
</form>

the connection to the table works, so i havnt put that code in.

there's no formatting for the html yet, i just want to get the php working first

Thanks

 

Link to comment
https://forums.phpfreaks.com/topic/212397-printing-values-to-a-drop-down-box/
Share on other sites

You are not running the query. Odd, since I would think you would get an error when trying to run mysql_fetch_row() on a string. Plus, the logic of reading the records is all wrong.

 

Try this

<?php

$query = "SELECT section_title FROM section_main";
$result = mysql_query($query) or die(mysql_error());
$titleOptions = '';
while ($row = mysql_fetch_assoc($query))
{
    $titleOptions .= "<option value=\"{$row['section_title']}\">{$row['section_title']}</option>\n";
}

?>

<form name="add_sub_section" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Sub Section Name <input type="text" name="sub_section_title" maxlength="200" />
<br /><br />
Sub Section desc <textarea name="sub_section_desc" rows="4" columns="30" /></textarea>
<br /><br />
SECTION 
<select name="section_title">
<?php echo $titleOptions; ?>
</select>
<br/><br/>
<input type="submit" name="submit" value="Add Section" />
</form>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.