create drop down with current month name selected


If you are looking to create a drop down menu with the current month’s name selected, and the current year for that matter, you have arrived at the right place.  The following script will demonstrates two html drop down lists populated with the months of the year and an array of years.  As time passes, the years that display will only go back three years and forward one.  So if you are in 2012, in the list you’ll have 2009 through 2013.  Three years back and 1 forward.  You can modify how many years back and forward you want to display in the drop down list simply by modifying the values added and subtracted in the variables $minYear and $maxYear.

    <form id=”frmSelectPd” name=”SelectPeriod” method=”get” action=”different_page.php”>

      Select a collection period:

      <select name=”month” id=”collection_period”>

                  <?php

                 $monthName = array(January, February, March, April, May, June, July, August, September,              

                                      October, November, December);

                                                   

                 for ($i=0; $i < count($monthName); $i++)

                          {

                                   $mn = 1 + $i;

                                   if($mn == date(m))

                                            {

                                                    echo <option selected value= . $mn . > . $monthName[$i] . </option> \n;

                                            }

                                   else

                                            {

                                                    echo <option value= . $mn . > . $monthName[$i] . </option> \n;

                                            }

                          }

                 ?> 

      </select>

      <select name=”year”>

                          <?php

                                  

                                   $year = date(Y);

                                   $minYr = $year3;

                                   $maxYr = $year + 2;

                                  

                                   echo $year;

                                   echo <br /> . $minYr . <br /> . $maxYr . <br />;

                                  

                                   for($y=$minYr; $y < $maxYr; $y++)

                                   {

                                            if($y==$year)

                                            {

                                                    echo <option selected value . $y . > . $y . </option> \n;

                                            }

                                            else

                                            {

                                                    echo <option value . $y . > . $y . </option> \n;

                                            }

                                   }                        

                         

                          ?> 

                         

        </select>

        <input type=”submit” value=” Go   ” />

    </form>

 

If you have any questions, comments or requests, please post a comment.  Hope this helps.

Leave a comment

Filed under Computer - Technical

Comment on this

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s