创建月份和年份的下拉列表,并在php中选择

时间:2017-12-29 07:52:55

标签: javascript php

我有一个“按月搜索”功能,并在下拉(选择)字段中列出了月份和年份。到目前为止,这是完美的,但我需要在提交后显示所选的(月份和年份)表格。但是我无法做到如何在下拉列表中选择年份和月份。下面是我的php JavaScript代码。有人帮我解决问题

下面是月份的两个下拉列表。月份和年份显示为选定的。

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

到目前为止,这是完美的工作,但我需要在提交表单后显示所选的(月份和年份)。但我无法做到如何在下拉列表中选择年份和月份。下面是我的php java脚本代码。有人帮我解决了吗?

3 个答案:

答案 0 :(得分:0)

您应该能够使用'use strict'; let _ = require('lodash'); let async = require('async'); ClientesDAO.prototype.dadosORSSRSSession = function (clienteid, callback) { var client_id = new ObjectId.ObjectID(clienteid); this._connection.open(function (err, mongoClient) { mongoClient.collection('sessao', function (err, collection) { if (err) { console.log('err when establishing db connection', err); return; } collection.find({ cliente: client_id }).toArray(function (err, result) { async.forEachOf(result, function (value, key, callback) { let orsId = new ObjectId.ObjectID(value.dadosORS); let srsId = new ObjectId.ObjectID(value.dadosSRS); Promise.all([ getORSDetailsById(mongoClient, orsId), getSRSDetailsById(mongoClient, srsId) ]).then((results) => { callback(null, { ors_id: orsId, srs_id: srsId, ors: results[0], srs: results[1] }) }).catch((err) => { callback(err); }); }, function (err, result) { if (err) { return console.error(err.message); } let orsIds = _.map(result, (item) => { return item.ors_id; }); let srsIds = _.map(result, (item) => { return item.srs_id; }); let orsCliente = _.map(result, (item) => { return item.ors; }); let srsCliente = _.map(result, (item) => { return item.srs; }); console.log('orsIds', orsIds); console.log('srsIds', srsIds); console.log('orsCliente', orsCliente); console.log('srsCliente', srsCliente); }); mongoClient.close(); }); }); }); }; function getORSDetailsById(mongoClient, id) { return new Promise((resolve, reject) => { mongoClient.collection('registosORS', function (err, collection) { collection.find({ _id: id }).toArray(function (err, result) { if (err) { return reject(err); } resolve(result[0]); }) }); }); } function getSRSDetailsById(mongoClient, id) { return new Promise((resolve, reject) => { mongoClient.collection('registosSRS', function (err, collection) { collection.find({ _id: id }).toArray(function (err, result) { if (err) { return reject(err); } resolve(result[0]); }) }); }); } 数组中保存的值来查找每个选择菜单的值。但是,正如您已声明表单使用$_REQUEST一样,最好使用GET数组中的值,因为这会使POST方法保持空闲状态,并且如果有任何POST数据,则不会影响所选值( ajax等)

$_GET

答案 1 :(得分:0)

月份代码

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 ($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);
}

答案 2 :(得分:0)

试试此代码

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