define initially selected option
Moderator: General Moderators
define initially selected option
In php is there any way to define what is selected when the user enters the page. For example, I have a drop down list of months. I would like November to be the initially selected option. Can this be done with PHP?
done with HTML that is.
use something like this you should:
make that dynamic with php you could (to select the current month etc). But output to the client in HTML it must.
use something like this you should:
Code: Select all
<select name="month">
<option value="January">Januray</option>
..
<option value="November" selected="selected">November</option>
</select>untested this is...
Code: Select all
$mon = date("F");
echo "<select name=\"month\">";
echo "<option value=\"January\" ".($mon == "January" ? "selected=\"selected\"" : "").">January</option>";
echo "</select>";- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Code: Select all
$months = array(
'January',
'Febuary',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'November',
'December',
);
echo '<select name="month">';
foreach ($months as $month) {
echo '<option value="'.$month.'" '.($month == date('F', time()) ? 'selected="selected"' : '').'>'.$month.'</option>';
}
echo '</select>';