如何使用月份和年份填充下拉表单?

我正在尝试用 HTML 和 PHP 创建两个下拉菜单。首先是一个自动提交问题,它自动填充会话变量为一个月的时间:

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

这个实现得非常好,但是我需要第二个下拉列表,写上日期和日期:

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

会话变量使用了一个临时日期“2014”,这是我之前设置的。我需要删除这个年度的会话变量,并获得第一个下拉列表,以知道选择的月份是哪一年,然后将其传递给临时日期,以便它能够正确填充下一年(2015年)的日期名称和号码(如果您选择1月起)。目前,它只显示当前年份(2014年)的日期名称和编号。

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>';
?>
<?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.