如何在php中合并选择选项名称

时间:2018-12-18 02:37:37

标签: php html

http://10.10.55.25/test/index.php?getdate=2018&getdate=05

我有2个下拉选项。年和月。我单击搜索后的结果是“ getdate = 2018&getdate = 05”,但我希望结果是“ getdate = 201805”

<?php
    echo "<select name=getdate>";
    for($i=0;$i<=11;$i++)
    {
    $year=date('Y',strtotime("last day of -$i year"));
    echo "<option>$year</option>";
    }
    echo "</select>";

    echo "<select name=getdate>";
    for($i=0;$i<=11;$i++){
    $month=date('m',strtotime("first day of -$i month"));
    echo "<option>$month</option> ";
    }
    echo "</select>";
    ?>

    <button type="submit" class="btn"><?php echo "Search";?></button>

screenshoot

1 个答案:

答案 0 :(得分:0)

您需要隐藏输入和JS来连接年份和月份的值

PHP

<?php
echo "<select id='year'>";
for($i=0;$i<=11;$i++)
{
$year=date('Y',strtotime("last day of -$i year"));
echo "<option>$year</option>";
}
echo "</select>";

echo "<select id='month'>";
for($i=0;$i<=11;$i++){
$month=date('m',strtotime("first day of -$i month"));
echo "<option>$month</option> ";
}
echo "</select>";
?>

<input type="hidden" name="getdate">
<button type="submit" class="btn"><?php echo "Search";?></button>

JS

<script type="text/javascript">
$('select#year, select#month').change(function() {
    yr = $('select#year option:selected').text();
    mnth = $('select#month option:selected').text();

    $('input[name="getdate"]').val(yr+""+mnth);
});
</script>