Populate drop down form with months and year php

14.4k views Asked by At

I am trying to create two drop downs with HTML and PHP. The first is an auto submit that sends a month to a session variable:

<form action="" method="post">
      <select name="month" onchange="this.form.submit()">
        <option value="">-- Select Month --</option>
        <?php $month = date('n'); // current month
         for ($x = 0; $x < 12; $x++) { ?>
          <option value="<?php echo date('m', mktime(0,0,0,$month + $x,1)); ?>" 
          <?php echo date('m', mktime(0,0,0,$month + $x,1)) == $_SESSION['month'] ? "selected='selected'":""; ?> >
          <?php echo date('F Y', mktime(0,0,0,$month + $x,1)); ?>
         <?php } ?>
      </select>
    </form>

This works great. But I then need a second drop down with the day name and number:

<form action="" method="post">
      <select name="day" onchange="this.form.submit()">
        <option value="">-- Select Day --</option>
        <?php for ($i = $current_day; $i < 31; $i++) { 
        $tmp_date = $_SESSION['year']."/".$_SESSION['month']."/".$i; 
              $weekday = date('D', strtotime($tmp_date));  { ?>
        <option value="<?php echo $i; ?>" <?php echo $i == $_SESSION['day'] ? "selected='selected'":""; ?> >
        <?php echo $weekday." ".$i; ?>
     <?php  } ?>  
     <?php } ?>
        </option>
        <?php } ?>
      </select>
    </form>

This uses a temp date with a session variable as '2014' which I set before. I need to remove the session year variable and get the first drop down to know which year the month selected is and then pass this to the temp date so that it populates the day name and number correctly for the next year (2015) if you choose January onwards. At the moment it only shows the day name and number from the current year (2014) in this case.

3

There are 3 answers

2
Horse SMith On

I had to check the print out of the first script, because it didn't make much sense...

<form action="" method="post">
      <select name="month" onchange="this.form.submit()">
        <option value="">-- Select Month --</option>
                  <option value="11" 
           >
          November 2014                   <option value="12" 
           >
          December 2014                   <option value="01" 
           >
          January 2015                   <option value="02" 
           >
          February 2015                   <option value="03" 
           >
          March 2015                   <option value="04" 
           >
          April 2015                   <option value="05" 
           >
           May 2015                   <option value="06" 
           >
          June 2015                   <option value="07" 
               >
          July 2015                   <option value="08" 
           >
          August 2015                   <option value="09" 
           >
          September 2015                   <option value="10" 
           >
          October 2015               </select>
    </form> 

Might be I'm missing something, because it seemed to work, but it doesn't seem like you're closing the option tags. You are doing this:

<option value="11">November 2014
<option value="12">December 2014

Instead of this:

<option value="11">November 2014</option>
<option value="12">November 2014</option>

This doesn't seem to be the issue though, at least not in chrome, because when I checked the head data being sent, it was just fine:

month: 11

What happens when you send in the variable is that whatever page you send this form data with the method POST against will have to handle it further on. In PHP you do that with the global variable $_POST, in this case $_POST['month'].

if(!empty($_POST['month']))
{
    echo $_POST['month'];
}

If you want to make it a session variable, the page you send the form data to will have to communicate with your browser to set this (through the reply HTTP header field). To do anything related to session in PHP you must first start a session using the session_start() before any HTML/data is sent against the browser, and the same goes for if you want to set a session variable using for example session_set_cookie_params. You also got setcookie, which also must be used before any data/HTML is sent to the browser and the variables can be accessed through $_COOKIE.

See if you can make some sense out of this, I've just used $_POST in this case, you can swap it with session or cookie and remember to send it before any data/html to the browser.

<?php
$selmonth = 0;
if(!empty($_POST['month']))
{
    $selmonth = $_POST['month'];
}
$month = date('n'); // current month
echo '<form action="" method="post">
<select name="month" onchange="this.form.submit()">
    <option value="">-- Select Month --</option>';
for($i=0; $i<12; $i++)
{
    $monthtime = mktime(0,0,0,$month + $i,1);
    $monthnum = date('m', $monthtime);
    echo '
    <option value="'.$monthnum.'"'.
    ($monthnum == $selmonth ? ' selected="selected"' : '').
    '>'.date('F Y', $monthtime).'</option>';
}
echo '
</select>
</form>';
?>
1
Pete Naylor On
<?php
if(isset($_POST)) {
    $date = explode('-', $_POST['month']);
    $year = $date[0];
    $month = $date[1];
    echo "Year: ".$year." Month: ".$month;
}
?>

<form action="" method="post">
<select name="month" onchange="this.form.submit()">
<?php
  for ($i = 0; $i <= 12; ++$i) {
    $time = strtotime(sprintf('+%d months', $i));
    $value = date('Y-m', $time);
    $label = date('F Y', $time);
    printf('<option value="%s">%s</option>', $value, $label);
  }
  ?>
</select>

This gives me the year and month as separate values.

0
PHP Laravel Developer On
  <select required="" class="btn btn-warning btn-raised" onchange="loadMonthYear(this)">
     <?php
        $fdt = date('Y-m-1');
        $tdt = date('Y-m-1', strtotime("-12 months"));
        while (date('Ymd', strtotime($fdt)) >= date('Ymd', strtotime($tdt))) {
                    
            echo '<option value="'.date('M, Y', strtotime($fdt)).'"><a data-tag="' . date('M, Y', strtotime($fdt)) . '" href="javascript: void(0);">' . date('M, Y', strtotime($fdt)) . '</option>';      
            $fdt = date('Y-m-1', strtotime("-1 months", strtotime($fdt)));
        }
    ?>               
</select>