0

我正在尝试根据用户选择的年份向用户显示选项。在文件的顶部,我有一个检索年份的功能,并将其发送回 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;

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

4

1 回答 1

0

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

if (isset($_POST['submit2'])) {
    $year = htmlentities($_POST['year']);
    //header('Location: view2.php?$year=".$year."');
    //echo $year;
}
于 2013-11-15T09:58:23.437 回答