Page 1 of 1

Select box problem...

Posted: Fri Jun 26, 2009 4:16 pm
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?

Re: Select box problem...

Posted: Fri Jun 26, 2009 4:45 pm
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>";

Re: Select box problem...

Posted: Fri Jun 26, 2009 5:02 pm
by psicloone
Thank you! :D