Select box problem...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
psicloone
Forum Newbie
Posts: 5
Joined: Mon Jun 15, 2009 9:13 am

Select box problem...

Post by psicloone »

When I use select box to input data in the database it shows the default first option, after I hit submit (but it is the selected option that goes in the database).

How can I make it to show the selected value, after I hit submit, instead of the default first?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Select box problem...

Post by requinix »

Example:

Code: Select all

<?php
 
$option = ""; // something that won't appear in the list of options
if (/* form was submitted */) {
    // blah blah blah
    $option = $_POST["option"];
    // blah blah blah
}
 
echo "<form>";
echo "<select name='option'>";
foreach ($list_of_options as $value) {
    echo "<option value='$value'";
    if ($option == $value) echo " selected='selected'"; // compare the current with the selected
    echo ">$value</option>";
}
echo "</select>";
echo "</form>";
psicloone
Forum Newbie
Posts: 5
Joined: Mon Jun 15, 2009 9:13 am

Re: Select box problem...

Post by psicloone »

Thank you! :D
Post Reply