jeff5656 Posted December 1, 2008 Share Posted December 1, 2008 Lets say I assign this to current month: $curr_month = date ('F'); How do I write this code so that the current month is selected? <form name="pickmonth" method="post" action="../schedules/editmonth.php"> <select name = "which_month" > <option value = "" >Select Month <option value = "January" >January <option value = "February">February <option value = "March">March <option value = "April">April <option value = "May">May <option value = "June">June <option value = "July">July <option value = "August">August <option value = "September">September <option value = "October">October <option value = "November">November <option value = "December">December </select></td> <td><input type="submit" value="Submit month" /></td> </form> Link to comment https://forums.phpfreaks.com/topic/135000-solved-select-current-month/ Share on other sites More sharing options...
rhodesa Posted December 1, 2008 Share Posted December 1, 2008 <form name="pickmonth" method="post" action="../schedules/editmonth.php"> <select name="which_month"> <option value="">Select Month</option> <?php $curr_month = date('F'); for($n=1;$n <=12;$n++){ $month = date('F',strtotime(date('Y-'.$n.'-1'))); printf(" <option value=\"%s\" %s>%s</option>\n",$month,$month==date('F')?'SELECTED':'',$month); } ?> </select> </td> <td><input type="submit" value="Submit month" /></td> </form> Link to comment https://forums.phpfreaks.com/topic/135000-solved-select-current-month/#findComment-703099 Share on other sites More sharing options...
tapos Posted December 1, 2008 Share Posted December 1, 2008 u can use following: <?php $months = array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'); $curr_month = date ('F'); ?> <form name="pickmonth" method="post" action="../schedules/editmonth.php"> <select name = "which_month" > <option value = "" >Select Month <?php foreach($months as $month){ $selected = ($month == $curr_month) ? 'selected="selected"' : ''; echo '<option '.$selected.' value = "'.$month.'" >'.$month.'</option>'; } ?> </select> <input type="submit" value="Submit month" /> </form> Link to comment https://forums.phpfreaks.com/topic/135000-solved-select-current-month/#findComment-703101 Share on other sites More sharing options...
jeff5656 Posted December 1, 2008 Author Share Posted December 1, 2008 Thanks I chose the code by rhodesa and it works. One oddity: in microsoft explorer the drop down says "December" as selected, but in Firefox it still says "Select month" in the drop down. Even refreshing the screen does not help. Any ideas? Link to comment https://forums.phpfreaks.com/topic/135000-solved-select-current-month/#findComment-703110 Share on other sites More sharing options...
PFMaBiSmAd Posted December 1, 2008 Share Posted December 1, 2008 'SELECTED' should be 'selected="selected"' to make valid HTML. Link to comment https://forums.phpfreaks.com/topic/135000-solved-select-current-month/#findComment-703112 Share on other sites More sharing options...
rhodesa Posted December 1, 2008 Share Posted December 1, 2008 Thanks I chose the code by rhodesa and it works. One oddity: in microsoft explorer the drop down says "December" as selected, but in Firefox it still says "Select month" in the drop down. Even refreshing the screen does not help. Any ideas? Try hitting Ctrl-F5 to force a full refresh of the page. It might be caching your form selections Link to comment https://forums.phpfreaks.com/topic/135000-solved-select-current-month/#findComment-703373 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.