I want a html with a dropdown menu were it's items are picked up from a mysql database. Then, when I select one of them, I want to be able to use it on a php file. Anybody has an example? Thanks
2 Answers
print '<form method="post" action="submit.php">';
print '<select size="1" name="myselect">';
foreach($dbresults as $r) {
print '<option value="'.$r['field'].'">'.$r['field'].'</option>';
}
print '</select>';
print '<input type="submit" value="Go!" />';
print '</form>';
And on the form's action page:
$chosen_thing = $_POST['myselect'];
2 Comments
aocferreira
Grim but how can I use foreach($dbresults as $r) { print '<option value="'.$r['field'].'">'.$r['field'].'</option>'; } in html file? isn't that php code?
Grim...
Yes - so put it in php tags. You can mix HTML and php on the same page (give it a .php extension) - although this isn't 'best practice', I wouldn't worry about that right now.