Truemag

  • Categories
    • Tips And Tricks
    • Internet
    • PHP
    • Javascript
    • CSharp
    • SQL Server
    • Linux
  • Lastest Videos
  • Our Demos
  • About
  • Contact
  • Home
  • Write With Us
  • Job Request
Home PHP PHP Create Month & Year Drop Down List With Current Month & Year

PHP Create Month & Year Drop Down List With Current Month & Year

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>

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

PHP Create Month & Year Drop Down List With Current Month & Year

PHP Create Month & Year Drop Down List With Current Month & Year

Oct 9, 2014Hoan Huynh

Source Code Demo page

We've launched completely new templateHow To Connect To VPN Server On Windows
You Might Also Like:
  • Xcode get current date, month, year
  • How To Group Related Options In A Drop Down List
  • SQL Server get first/last day of previous/current/next month
  • Create Auto Refresh Page Or Redirect After A Given Seconds
  • Mysql select current date, current time, current TIMESTAMP example
  • JQuery Get selected value of radio button
  • Display User Current Location On Google Map
  • Validate Date With JQuery And Date Object
  • PHP Load And Save Facebook Friend List
  • JavaScript Get Radio Button Value
Hoan Huynh

Hoan Huynh is the founder and head of 4rapiddev.com. Reach him at [email protected]

6 years ago PHPdrop down list, SELF3,043
0
GooglePlus
0
Facebook
0
Twitter
0
Digg
0
Delicious
0
Stumbleupon
0
Linkedin
0
Pinterest
Most Viewed
PHP Download Image Or File From URL
22,237 views
Notepad Plus Plus Compare Plugin
How To Install Compare Text Plugin In Notepad Plus Plus
20,087 views
Microsoft SQL Server 2008 Attach Remove Log
Delete, Shrink, Eliminate Transaction Log .LDF File
15,872 views
JQuery Allow only numeric characters or only alphabet characters in textbox
13,343 views
C# Read Json From URL And Parse/Deserialize Json
9,851 views
4 Rapid Development is a central page that is targeted at newbie and professional programmers, database administrators, system admin, web masters and bloggers.
Recent Posts
  • Term Papers – Easy to Take Care of
  • How to Write Term Papers in Online Tutorials
  • How to Plan Writing an Essay
  • How to Apply For a Payday Loan With Bad Credit
  • Statistics For Sale – How To Compose One
Categories
  • CSharp (45)
  • Facebook Graph API (19)
  • Google API (7)
  • Internet (87)
  • iPhone XCode (8)
  • Javascript (35)
  • Linux (28)
  • MySQL (16)
  • PHP (84)
  • Problem Issue Error (29)
  • Resources (32)
  • SQL Server (25)
  • Timeline (5)
  • Tips And Tricks (141)
  • Uncategorized (123)
Recommended
  • Custom Software Development Company
  • Online Useful Tools
  • Premium Themes
  • VPS
2014 © 4 Rapid Development