我正在尝试创建一个“报告”,可以使用页面顶部的表单进行过滤。筛选结果的选项是财政年度,默认情况下是当前的FY,默认情况下都会检查多个类别(复选框)。页面使用默认数据正确生成,但是在提交表单时,页面将“刷新”但没有生成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; ?>" /> <?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." \n";
}
else if(in_array($catAllRow,$catArray)) {
echo "<input type=\"checkbox\" name=\"Category\" value=\"".$catAllRow."\" checked=\"checked\" />".$catAllRow." \n";
}
else {
echo "<input type=\"checkbox\" name=\"Category\" value=\"".$catAllRow."\" />".$catAllRow." \n";
}
}
?>
</fieldset> <br />
<input type="submit" value="submit" />
</form> <!-- End: filter -->
</div> <!-- End: filters -->
从这里开始,原始代码继续将结果输出到表格中,但这种方式正常,我不认为这是问题所在。如果被问到,我可以分享更多。
答案 0 :(得分:1)
我认为检查是否存在_POST值是错误的。试试这个:
if(isset($_POST['FiscalYear']))
看看是否有效
答案 1 :(得分:1)
如果要检查提交按钮,则需要命名
<input type="submit" value="submit" name="submit" />
否则php不会将它放在$ _POST数组
中答案 2 :(得分:1)
您需要为提交按钮指定一个名称,如果您正在使用它来检查表单是否已提交......
<input type="submit" value="submit" name="submit" />
或者,如果您不想更改提交按钮,则可以检查类别输入上的isset
if(isset($_POST['Category'])){echo"SET";} else{echo"NOT SET";}