1

我正在尝试创建一个“报告”,可以使用页面顶部的表单进行过滤。过滤结果的选项是会计年度,默认情况下是当前财政年度和多个类别(复选框),默认情况下都选中。该页面使用默认数据正确生成,但是当提交表单时,页面将“刷新”但没有生成 POST 数据。我尝试创建页面的副本并将其设置为操作 URL,但它仍然没有任何 POST 数据并使用默认值。我将在下面包含我的代码,并尝试将其缩小到必要的部分以使其更容易,但如果需要,可以共享所有代码。提前感谢您提供的任何帮助。

<body>
    <?php 
    if(isset($_POST['submit'])){echo"SET";} else{echo"NOT SET";}
// Establish Connection and Variables       
// Connection   
        include "./include/class/DBConnection.php";
        DBConnection::$dsn;
        DBConnection::$user;
        DBConnection::$pass;
        DBConnection::getDBConnection();
// The Current Fiscal Year
        $today = getdate();
        $month = $today['month'];
        // seperate first and second half of fiscal year
        $old = array('January','February','March','April','May','June');    
        if (in_array($month,$old)) {
            $year = $today['year'] + 1;
        }
        else {
            $year = $today['year'];
        }                       
// Create SQL Query Variables - Removed for post
// Set filter criteria
// Retrieve array of possible categories and create SQL WHERE statment  
        $catAllCxn = DBConnection::$cxn->prepare($SQL_Categories);
        $catAllCxn->execute();
        $catAllCxn->setFetchMode(PDO::FETCH_ASSOC);
        $catAllArray = array();
        while($catAllRow = $catAllCxn->fetch()) {
            $cat = $catAllRow['Category'];
            array_push($catAllArray, $cat);
        }
        $catAllInQuery = implode(',',array_fill(0,count($catAllArray),'?'));
// Create array for category filter IF form was submitted to itself
        if (isset($_POST['submit'])){  // if page is submitted to itself

            $catFilterArray = $_POST['Category'];
            $catFilterInQuery = implode(',',array_fill(0,count($catFilterArray),'?'));

        }
// Switch for ALL or Filtered report
        if(!isset($_POST['submit'])) { // if page is not submitted to itself 

            $FiscalYear = $year;
        //  $DiscludedDepartmentNumbers = "21117";
            $catArray = $catAllArray;
            $IncludedCategories = $catAllInQuery;
        }
        else {
            $FiscalYear = $_POST["FiscalYear"];
        //  $DiscludedDepartmentNumbers = "21117";
            $catArray = $catFilterArray;
            $IncludedCategories = $catFilterInQuery;
        }               
    ?>
<!-- Filter Form -->        
    <div id="filters" style="border: 1px solid;"> 
        <form name="filter" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="POST"> 
            Fiscal Year: <input type="text" name="FiscalYear" value="<?php echo $FiscalYear; ?>" /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?php if(isset($_POST['submit'])){echo"SET";} else{echo"NOT SET";}?>
            <br />
            <fieldset>
                <legend>Select Categories</legend>
                <?php 
                    foreach($catAllArray as $catAllRow) {
                        if (!isset($_POST['submit'])) {
                            echo "<input type=\"checkbox\" name=\"Category\" value=\"".$catAllRow."\" checked=\"checked\" />".$catAllRow."&nbsp;&nbsp;\n";
                        }   
                        else if(in_array($catAllRow,$catArray)) {
                            echo "<input type=\"checkbox\" name=\"Category\" value=\"".$catAllRow."\" checked=\"checked\" />".$catAllRow."&nbsp;&nbsp;\n";
                        }
                        else {
                            echo "<input type=\"checkbox\" name=\"Category\" value=\"".$catAllRow."\" />".$catAllRow."&nbsp;&nbsp;\n";
                        }   
                    }   
                ?>
            </fieldset> <br />
            <input type="submit" value="submit" />
        </form> <!-- End: filter -->
    </div>  <!-- End: filters -->

从这里开始,原始代码继续将结果输出到表格中,但这可以正常工作,我认为这不是问题。如果被问到,我可以分享更多。

4

3 回答 3

1

我认为您检查是否有 _POST 值是错误的。试试这个:

if(isset($_POST['FiscalYear']))

看看这是否有效

于 2012-06-19T20:07:13.590 回答
1

如果你想检查它,你需要命名你的提交按钮

<input type="submit" value="submit" name="submit" />

否则 php 不会把它放在 $_POST 数组中

于 2012-06-19T20:13:21.560 回答
1

您需要为提交按钮命名,如果这是您用来检查表单是否已提交的名称...

<input type="submit" value="submit" name="submit" />

或者,如果您不想更改提交按钮,则可以在类别输入中检查 isset

if(isset($_POST['Category'])){echo"SET";} else{echo"NOT SET";}
于 2012-06-19T20:14:24.107 回答