Page 1 of 1

define initially selected option

Posted: Wed Nov 30, 2005 11:19 am
by lcky13
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?

Posted: Wed Nov 30, 2005 11:28 am
by Burrito
done with HTML that is.

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>
make that dynamic with php you could (to select the current month etc). But output to the client in HTML it must.

Posted: Wed Nov 30, 2005 11:35 am
by lcky13
that is what I need to do, make it dynamic?
how do I accomplish this with php?

Posted: Wed Nov 30, 2005 11:53 am
by Burrito
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>";

Posted: Wed Nov 30, 2005 1:09 pm
by lcky13
<?php if($m == "January"){echo " selected";}?>

ended up just inserting this into my html

works!

Posted: Wed Nov 30, 2005 3:47 pm
by John Cartwright

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>';