Jump to content

[SOLVED] Select current month


jeff5656

Recommended Posts

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

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

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>

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.