PHP显示选项取决于所选年份

时间:2013-11-15 09:43:01

标签: php sql

我正在尝试根据用户选择的年份向用户显示选项。 在文件的顶部,我有一个函数来检索年份,并将其发送回view2.php的同一页面,但我确定我做错了:

if(!empty($_POST['year'])){
    $year = htmlentities($_POST['year']);
    header('Location: view2.php');
    //exit();
}

稍后,我有一个提交年份的表单

    <form method="post" action="">

    Select year:
    <?php
        $myOptions = array(
            'All' => 'All',
            '2013' => '2013', 
            '2014' => '2014', 
            '2015' => '2015', 
            '2016' => '2016', 
            '2017' => '2017', 
            '2018' => '2018', 
            '2019' => '2019', 
            '2020' => '2020' 

        );
    ?>
    <select name="year">
        <?php
        foreach($myOptions as $key => $opt) {
            $selected = null;

        if(isset($_POST['View All']) && $key === $_POST['View All'])
            $selected = ' selected';

        echo '<option value="' . $key . '"' . $selected. '>' . $opt . '</option>';
        }
        ?>  
    </select>       
    <input type="submit" name="submit" value="GO"/>
    </form>

如果我手动设置$year变量,我的SQL查询工作正常,所以函数($users->expensePerYear($username, $year, $i);)没问题。这是我的功能:

    <?php
    $i=0;
    $year = 2013;
        //if (isset($year)){
        $users->expensePerYear($username, $year, $i);
        $userQueryResult = $users->queryResult;
        $count = $users->i;
        $totalSpent = $users->totalSpent;
        $remaining = $budget-$totalSpent;

问题是我正在尝试将年变量发送到同一页面而我做错了。我希望用户在select中指定年份,将其发送到同一页面并根据该变量显示内容。

1 个答案:

答案 0 :(得分:0)

别介意我,我只需要评论header()行,它就可以了。

if (isset($_POST['submit2'])) {
    $year = htmlentities($_POST['year']);
    //header('Location: view2.php?$year=".$year."');
    //echo $year;
}