PHP从当前年份开始选择菜单

时间:2017-11-01 07:10:40

标签: php drop-down-menu

我正在使用Bootstrap和PHP。然而,formmail工作,下面代码中的最后一个选项值(年份)打印前一个选项值(日期)的值。我不知道我哪里出错了。

发布到电子邮件的值: 按月服务:1 按日服务:2 按年份服务:2(年与日相同)

<select id="Serve_by_Month" name="Serve_by_Month" required>
<option value="">Month*</option>
<?for($xx=1;$xx<13;$xx++){?>

<option value="<?=$xx?>"<?if($xx==$exp_mo)echo " selected";?>> 
<?=sprintf("%02d",$xx)?>
</option>
<?}?>
</select>
<br class="hidden-lg">
<select id="Serve_by_Day" name="Serve_by_Day" required>
<option value="">Day*</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
</select>

<select id="Serve_by_Year" name="Serve_by_Year" required>
<option value="">Year*</option>
<?
$year = date("Y");
$yearto = date("Y")+10;
while ($year <= $yearto)
{
echo "<option value='".$year."'>".$year."</option>";
$year++;
}
?>
</select>

2 个答案:

答案 0 :(得分:2)

自从我做了任何php以来已经有很长一段时间了,但这个问题是否与日期(“Y”)有关,返回一个字符串而不是一个int?

尝试将$ year分配更改为

$year = intval(date("Y"));
$yearto = $year+10;

答案 1 :(得分:1)

试试这个,它应该可以解决你的问题

<?php
    if(isset($_POST['submit-form'])){
        $selectedMonth = $_POST['Serve_by_Month']; //This will give you the month
        $selectedDay = $_POST['Serve_by_Day']; //This will give you the day
        $selectedYear = $_POST['Serve_by_Year']; //This will give you the year
        echo 'date: '.$selectedMonth.'-'.$selectedDay.'-'.$selectedYear;
        echo '<br>';
    }
?>

<form method="post">
    <select id="Serve_by_Month" name="Serve_by_Month" required>
        <option value="">Month*</option>
        <?php 
            for($i=1;$i<13;$i++){
                $selected = $i==$exp_mo ? 'selected' : '';
            ?>
            <option value="<?=$i?>" <?=$selected?>>
                <?=sprintf("%02d",$i)?>
            </option>
    <?php } ?>
    </select>

    <br class="hidden-lg">

    <select id="Serve_by_Day" name="Serve_by_Day" required>
        <option value="">Day*</option>
        <?php
            for($i = 1; $i <= 31; $i++){ ?>
                <option value="<?=$i?>"><?=$i?></option>              
            <?php }
        ?>
    </select>

    <select id="Serve_by_Year" name="Serve_by_Year" required>
        <option value="">Year*</option>
        <?php
        for($i = date('Y'); $i <= date('Y')+10; $i++){?>
            <option value="<?=$i?>"><?=$i?></option>
        <?php }
        ?>
    </select>

    <input type="submit" name="submit-form"/>
</form>