douwen3127 2017-12-29 07:52
浏览 95
已采纳

创建月份和年份的下拉列表,并在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

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?

  • 写回答

3条回答 默认 最新

  • dongxi4235 2017-12-29 08:09
    关注

    You should be able to use the value held in the $_REQUEST array to find the value of each select menu. 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 )

    <!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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 表达式必须是可修改的左值
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题