PHP code below will create a 2 drop down lists, 1 for month and 1 for year. They have pre-selected option with current month & current year.
- Month drop down list options run from 1 to 12
- Year drop down list options run from current year to current year + 10
- By default, pre-selected option will be current month & year. And pre-selected option will be changed when select another option.
- The form has POST method and submit to SELF
<?php $month = $_POST["ddlMonth"]; $year = $_POST["ddlYear"]; if($month == "") $month = date("m"); if($year == "") $year = date("Y"); ?> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Create Month & Year Drop Down List With Current Month & Year</title> <meta name="robots" content="noindex, nofollow" /> </head> <body> <form action="" method="post"> Month: <select id="ddlMonth" name="ddlMonth"> <?php for($i=1; $i<=12; $i++) { if($i == $month) echo '<option selected value="' . $i . '">' . $i . '</option>'; else echo '<option value="' . $i . '">' . $i . '</option>'; } ?> </select> <br> <br> Year: <select id="ddlYear" name="ddlYear"> <?php for($i=$year; $i<=$year+10; $i++) { if($i == $year) echo '<option selected value="' . $i . '">' . $i . '</option>'; else echo '<option value="' . $i . '">' . $i . '</option>'; } ?> </select> <br> <br> <button type="submit">Go</button> </form> </body> </html> |