简体   繁体   English

为月份和年份创建一个下拉列表并在 php 中选择

[英]create a dropdown list for month and year and selected in php

I have a "search by month and year " feature and have listed the months and year in a drop-down (select) field.so far this is working perfect but i need to show selected (month and year) after submitting the form .but i unable to do how to get selected year and month in drop down.below is my php JavaScript code .anyone help me how to solve我有一个“按月和年搜索”功能,并在下拉(选择)字段中列出了月和年。到目前为止,这是完美的,但我需要在提交表单后显示所选(月和年)。但我无法做如何在下拉列表中选择年份和月份。下面是我的 php JavaScript 代码。任何人都可以帮助我如何解决

below is the two dropdowns for month year .the month and year sholud be shown as selected.下面是月份年份的两个下拉菜单。月份和年份应该显示为选中状态。

<select name="month" class="form-control" >

<?php

for ($i = 0; $i <= 12; ++$i)
    {
    $time = strtotime(sprintf('+%d months', $i));
    $label = date('F ', $time);
    $value = date('m', $time);
    printf('<option value="%s" selected="selected">%s</option>', $value, $label);
    }

?>
</select>
<select name="year" class="form-control">
      <?php

for ($i = 0; $i <= 12; ++$i)
    {
    $time = strtotime(sprintf('-%d years', $i));
    $value = date('Y', $time);
    $label = date('Y ', $time);
    printf('<option value="%s" selected="selected">%s</option>', $value, $label);
    }

?>
</select>

so far this is working perfect but i need to show selected (month and year) after submitting the form .but i unable to do how to get selected year and month in drop down.below is my php java-script code .anyone help me how to solve it?到目前为止,这是完美的,但我需要在提交表单后显示选定的(月份和年份)。但我无法做如何在下拉列表中选择年份和月份。下面是我的 php java 脚本代码。任何人都可以帮助我如何解决?

Try this code试试这个代码

<select name="month" class="form-control" >
<?php
    for ($i = 1; $i <= 12; ++$i){
        $time = strtotime(sprintf('+%d months', $i));
        $label = date('F ', $time);
        $value = date('m', $time);
        echo '<option value="'.$value.'" ';
        if((isset($_GET['month']))&&($value==$_GET['month']))echo 'selected';// Check if form submitted or not. select the month if yes
        echo '>'.$label.'</option>';
    }
?>
</select>
<select name="year" class="form-control">
<?php
    for ($i = 0; $i <= 12; ++$i){
        $time = strtotime(sprintf('-%d years', $i));
        $value = date('Y', $time);
        echo '<option value="'.$value.'" ';
        if((isset($_GET['year']))&&($value==$_GET['year']))echo 'selected';// Check if form submitted or not. select the year if yes
        echo '>'.$value.'</option>';
    }
?>
</select>

You should be able to use the value held in the $_REQUEST array to find the value of each select menu.您应该能够使用$_REQUEST数组中保存的值来查找每个选择菜单的值。 However as you have stated the form uses GET it is preferable to then use values from the $_GET array as this leaves the POST method free and will not affect the selected values should there be any POSTing of data ( ajax etc )但是,正如您所说,表单使用GET之后最好使用$_GET数组中的值,因为这样可以使 POST 方法空闲,并且如果有任何数据发布(ajax 等),则不会影响所选值

<!doctype html>
<html>
    <head>
        <meta charset='utf-8' />
        <title></title>
    </head>
    <body>
        <form>
            <select name="month" class="form-control">
                <?php
                    $month = !empty( $_GET['month'] ) ? $_GET['month'] : 0;
                    for ($i = 0; $i <= 12; ++$i) {
                        $time = strtotime(sprintf('+%d months', $i));
                        $label = date('F ', $time);
                        $value = date('m', $time);

                        $selected = ( $value==$month ) ? ' selected=true' : '';

                        printf('<option value="%s"%s>%s</option>', $value, $selected, $label );
                    }
                ?>
            </select>

            <select name="year" class="form-control">
                <?php

                    $year = !empty( $_GET['year'] ) ? $_GET['year'] : 0;

                    for ($i = 0; $i <= 12; ++$i)  {
                        $time = strtotime(sprintf('-%d years', $i));
                        $value = date('Y', $time);
                        $label = date('Y ', $time);

                        $selected = ( $value==$year ) ? ' selected=true' : '';

                        printf('<option value="%s"%s>%s</option>', $value, $selected, $label);
                    }
                ?>
            </select>
            <input type='submit' />
        </form>
    </body>
</html>

For month code对于月份代码

for ($i = 0; $i <= 12; ++$i){
    $time = strtotime(sprintf('+%d months', $i));
    $label = date('F ', $time);
    $value = date('m', $time);
    $sleceted='';
    if(isset($_REQUEST['month']){
        $sleceted=$_REQUEST['month']== $value?'selected:selected':'';
    }
    printf('<option value="%s" '.$sleceted.'>%s</option>', $value, $label);
}

for year code对于年份代码

for ($i = 0; $i <= 12; ++$i){
    $time = strtotime(sprintf('+%d months', $i));
    $label = date('F ', $time);
    $value = date('m', $time);
    $sleceted='';
    if(isset($_REQUEST['year']){
        $sleceted=$_REQUEST['year']== $value?'selected:selected':'';
    }
    printf('<option value="%s" '.$sleceted.'>%s</option>', $value, $label);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM