将变量从一个文件传递到另一个文件 (PHP)
Passing variables from one file to another (PHP)
所以我正在创建此报告页面,用户可以在其中获得 select 报告类型和日期。他们单击提交按钮后,将显示 2 个新按钮,一个用于下载 excel 格式的报告,另一个用于下载 PDF 格式的报告。根据报告的类型 selected,我有将处理报告的特定文件。我已经测试了特定文件,当我使用硬编码日期时,它们确实可以正确生成报告。但是我希望能够使用用户 selected 的日期。请查看我的代码,让我知道我缺少什么。谢谢!!
saereport.php:
<!DOCTYPE html>
<HTML lang="en">
<HEAD>
<meta charset="utf-8">
<link rel="stylesheet" href="css/style2.css">
<TITLE>SAE Report</TITLE>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
$().ready(function() {
$(".datepicker").datepicker({dateFormat:'yy-mm-dd'});
});
</script>
</HEAD>
<BODY>
<center>
<h1>SAE Report</h1>
</center>
<form action = "" method = "post">
<label>Report Type</label>
<select id="report" name="report">
<option value="none"></option>
<option value="new">New SAEs Report</option>
<option value="cumulative">Cumulative SAE Report</option>
</select>
<label>Start Date</label><input type="text" class="datepicker" name="start">
<label>End Date</label><input type="text" class="datepicker" name="end">
<input type="submit" name="submit" value="Submit">
</form>
</BODY>
</HTML>
<?php
$type='';
$start='';
$end='';
if (isset($_post['submit'])){
$type=$_post['report'];
$start=$_post['start'];
$end=$_post['end'];
if ($type=="cumulative"){
echo "<form action='cumulativeRptExcel2.php?='.$end. method='get' name ='xls'>";
echo "<input type='submit' name='submitXLS' value='Download Excel'/>";
echo "</form>";
echo "<form action='$cumulativePDF' method='post' name ='xls'>";
echo "<input type='submit' name='submitXLS' value='Download PDF'/>";
echo "</form>";
}
elseif($type=='new' and $startdt!='' and $enddt!=''){
echo "<form action='$newXLS' method='get' name ='xls'>";
echo "<input type='submit' name='submitXLS' value='Download Excel'/>";
echo "</form>";
echo "<form action='$newPDF' method='get' name ='xls'>";
echo "<input type='submit' name='submitXLS' value='Download PDF'/>";
echo "</form>";
}
elseif($type="new" and ($start=='' or $end=='')){
echo "You need to select START and END date for the report";
}
}
?>
我的开头cumulativeRptExcel2.php:
<?php
include ('connect.php');
$endDT='';
if (isset($_get['enddt'])){
$endDT=$_get['enddt'];
}
$query="SELECT * from sae_cumulative_report2($endDT)";
$result = pg_query($db, $query);
...
?>
代码前的几点评论:
变量名区分大小写。使用 $_POST
而不是 $_post
,如 @toh19 所述;
参考:http://php.net/manual/en/language.variables.basics.php
不要直接相信用户输入。在这种情况下,为了更安全,您可以根据正则表达式验证用户输入。
参考:http://www.phptherightway.com/#data_filtering
代码:
<!DOCTYPE html>
<HTML lang="en">
<HEAD>
<meta charset="utf-8">
<link rel="stylesheet" href="css/style2.css">
<TITLE>
SAE Report
</TITLE>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
$().ready(function() {
$(".datepicker").datepicker({
dateFormat:'yy-mm-dd'}
);
}
);
</script>
</HEAD>
<BODY>
<center>
<h1>
SAE Report
</h1>
</center>
<form action = "" method = "post">
<label>Report Type</label>
<select id="report" name="report">
<option value="none"></option>
<option value="new">New SAEs Report</option>
<option value="cumulative">Cumulative SAE Report</option>
</select>
<label>Start Date</label>
<input type="text" class="datepicker" name="start">
<label>End Date</label>
<input type="text" class="datepicker" name="end">
<input type="submit" name="submit" value="Submit">
</form>
</BODY>
</HTML>
<?php
$type='';
$start='';
$end='';
if (isset($_POST['submit'])){
$type=$_POST['report'];
$start=$_POST['start'];
$end=$_POST['end'];
if ( $type == 'cumulative') {
echo "<form action='cumulativeRptExcel2.php?&enddt=$end' method='POST' name='xls'>";
echo "<input type='submit' name='submitXLS' value='Download Excel'/>";
echo "</form>";
echo "<form action='$cumulativePDF' method='post' name ='xls'>";
echo "<input type='submit' name='submitXLS' value='Download PDF'/>";
echo "</form>";
}
elseif ($type == 'new') {
if (empty($start) || empty($end)) {
die("You need to select START and END date for the report");
}
echo "<form action='$newXLS' method='get' name ='xls'>";
echo "<input type='submit' name='submitXLS' value='Download Excel'/>";
echo "</form>";
echo "<form action='$newPDF' method='get' name ='xls'>";
echo "<input type='submit' name='submitXLS' value='Download PDF'/>";
echo "</form>";
}
}
更新
- 之前的代码(在累积类型的表单动作中)存在语法问题;
- 如果你想通过查询字符串(
cumulativeRptExcel2.php?&enddt=...
)传递变量,你必须将形式METHOD
更改为POST
,否则(GET方法)你需要创建一个enddt 值的隐藏字段;
由于我无法生成按钮,因此用户可以 select 下载他们想要的文件(xls 或 pdf)。现在,我只是自动下载了 Excel 文件。这是代码:
<!DOCTYPE html>
<HTML lang="en">
<HEAD>
<meta charset="utf-8">
<link rel="stylesheet" href="css/style2.css">
<TITLE>SAE Report</TITLE>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
$().ready(function() {
$(".datepicker").datepicker({dateFormat:'yy-mm-dd'});
});
</script>
</HEAD>
<BODY>
<center>
<h1>SAE Report</h1>
</center>
<form action = "processReport.php" method = "GET">
<label>Report Type</label>
<select id="report" name="report">
<option value="none"></option>
<option value="new">New SAEs Report</option>
<option value="cumulative">Cumulative SAEs Report</option>
</select>
<label>Start Date</label><input type="text" class="datepicker" name="start">
<label>End Date</label><input type="text" class="datepicker" name="end">
<input type="submit" name="submit" value="Submit">
</form>
</BODY>
processReport.php:
$type='';
$startdt='';
$enddt='';
if (isset($_GET['submit'])){
$type=$_GET['report'];
$startdt=$_GET['start'];
$enddt=$_GET['end'];
if ($type=="cumulative" and $enddt!=''){
$query="SELECT * from sae_cum_decoded('$enddt')";
$filename='SAE_CumulativeReport_';
$insideTitle="Cumulative SAEs Report";
processFile($db,$filename, $insideTitle,$query);
}
elseif($type=='new' and $startdt!='' and $enddt!=''){
$query="SELECT * from sae_new_decoded('$startdt','$enddt')";
$filename='SAE_NewReport_';
$insideTitle="New SAEs Report";
processFile($db,$filename, $insideTitle,$query);
}
else
echo '<script language="javascript">';
echo 'alert("In order to generate a report, a REPORT TYPE needs to be selected.\nFor Cumulative SAEs Report, END DATE is required.\nFor New SAEs Report, START DATE and END DATE are required.")
window.location.href="saereport5.php"';
echo '</script>';
}
function processFile($db,$filename, $insideTitle, $query){
...}
最好弄清楚如何使用这些按钮来执行此操作,因为将来用户将需要同时拥有这两个选项。
所以我正在创建此报告页面,用户可以在其中获得 select 报告类型和日期。他们单击提交按钮后,将显示 2 个新按钮,一个用于下载 excel 格式的报告,另一个用于下载 PDF 格式的报告。根据报告的类型 selected,我有将处理报告的特定文件。我已经测试了特定文件,当我使用硬编码日期时,它们确实可以正确生成报告。但是我希望能够使用用户 selected 的日期。请查看我的代码,让我知道我缺少什么。谢谢!!
saereport.php:
<!DOCTYPE html>
<HTML lang="en">
<HEAD>
<meta charset="utf-8">
<link rel="stylesheet" href="css/style2.css">
<TITLE>SAE Report</TITLE>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
$().ready(function() {
$(".datepicker").datepicker({dateFormat:'yy-mm-dd'});
});
</script>
</HEAD>
<BODY>
<center>
<h1>SAE Report</h1>
</center>
<form action = "" method = "post">
<label>Report Type</label>
<select id="report" name="report">
<option value="none"></option>
<option value="new">New SAEs Report</option>
<option value="cumulative">Cumulative SAE Report</option>
</select>
<label>Start Date</label><input type="text" class="datepicker" name="start">
<label>End Date</label><input type="text" class="datepicker" name="end">
<input type="submit" name="submit" value="Submit">
</form>
</BODY>
</HTML>
<?php
$type='';
$start='';
$end='';
if (isset($_post['submit'])){
$type=$_post['report'];
$start=$_post['start'];
$end=$_post['end'];
if ($type=="cumulative"){
echo "<form action='cumulativeRptExcel2.php?='.$end. method='get' name ='xls'>";
echo "<input type='submit' name='submitXLS' value='Download Excel'/>";
echo "</form>";
echo "<form action='$cumulativePDF' method='post' name ='xls'>";
echo "<input type='submit' name='submitXLS' value='Download PDF'/>";
echo "</form>";
}
elseif($type=='new' and $startdt!='' and $enddt!=''){
echo "<form action='$newXLS' method='get' name ='xls'>";
echo "<input type='submit' name='submitXLS' value='Download Excel'/>";
echo "</form>";
echo "<form action='$newPDF' method='get' name ='xls'>";
echo "<input type='submit' name='submitXLS' value='Download PDF'/>";
echo "</form>";
}
elseif($type="new" and ($start=='' or $end=='')){
echo "You need to select START and END date for the report";
}
}
?>
我的开头cumulativeRptExcel2.php:
<?php
include ('connect.php');
$endDT='';
if (isset($_get['enddt'])){
$endDT=$_get['enddt'];
}
$query="SELECT * from sae_cumulative_report2($endDT)";
$result = pg_query($db, $query);
...
?>
代码前的几点评论:
变量名区分大小写。使用
$_POST
而不是$_post
,如 @toh19 所述;
参考:http://php.net/manual/en/language.variables.basics.php不要直接相信用户输入。在这种情况下,为了更安全,您可以根据正则表达式验证用户输入。
参考:http://www.phptherightway.com/#data_filtering
代码:
<!DOCTYPE html>
<HTML lang="en">
<HEAD>
<meta charset="utf-8">
<link rel="stylesheet" href="css/style2.css">
<TITLE>
SAE Report
</TITLE>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
$().ready(function() {
$(".datepicker").datepicker({
dateFormat:'yy-mm-dd'}
);
}
);
</script>
</HEAD>
<BODY>
<center>
<h1>
SAE Report
</h1>
</center>
<form action = "" method = "post">
<label>Report Type</label>
<select id="report" name="report">
<option value="none"></option>
<option value="new">New SAEs Report</option>
<option value="cumulative">Cumulative SAE Report</option>
</select>
<label>Start Date</label>
<input type="text" class="datepicker" name="start">
<label>End Date</label>
<input type="text" class="datepicker" name="end">
<input type="submit" name="submit" value="Submit">
</form>
</BODY>
</HTML>
<?php
$type='';
$start='';
$end='';
if (isset($_POST['submit'])){
$type=$_POST['report'];
$start=$_POST['start'];
$end=$_POST['end'];
if ( $type == 'cumulative') {
echo "<form action='cumulativeRptExcel2.php?&enddt=$end' method='POST' name='xls'>";
echo "<input type='submit' name='submitXLS' value='Download Excel'/>";
echo "</form>";
echo "<form action='$cumulativePDF' method='post' name ='xls'>";
echo "<input type='submit' name='submitXLS' value='Download PDF'/>";
echo "</form>";
}
elseif ($type == 'new') {
if (empty($start) || empty($end)) {
die("You need to select START and END date for the report");
}
echo "<form action='$newXLS' method='get' name ='xls'>";
echo "<input type='submit' name='submitXLS' value='Download Excel'/>";
echo "</form>";
echo "<form action='$newPDF' method='get' name ='xls'>";
echo "<input type='submit' name='submitXLS' value='Download PDF'/>";
echo "</form>";
}
}
更新
- 之前的代码(在累积类型的表单动作中)存在语法问题;
- 如果你想通过查询字符串(
cumulativeRptExcel2.php?&enddt=...
)传递变量,你必须将形式METHOD
更改为POST
,否则(GET方法)你需要创建一个enddt 值的隐藏字段;
由于我无法生成按钮,因此用户可以 select 下载他们想要的文件(xls 或 pdf)。现在,我只是自动下载了 Excel 文件。这是代码:
<!DOCTYPE html>
<HTML lang="en">
<HEAD>
<meta charset="utf-8">
<link rel="stylesheet" href="css/style2.css">
<TITLE>SAE Report</TITLE>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
$().ready(function() {
$(".datepicker").datepicker({dateFormat:'yy-mm-dd'});
});
</script>
</HEAD>
<BODY>
<center>
<h1>SAE Report</h1>
</center>
<form action = "processReport.php" method = "GET">
<label>Report Type</label>
<select id="report" name="report">
<option value="none"></option>
<option value="new">New SAEs Report</option>
<option value="cumulative">Cumulative SAEs Report</option>
</select>
<label>Start Date</label><input type="text" class="datepicker" name="start">
<label>End Date</label><input type="text" class="datepicker" name="end">
<input type="submit" name="submit" value="Submit">
</form>
</BODY>
processReport.php:
$type='';
$startdt='';
$enddt='';
if (isset($_GET['submit'])){
$type=$_GET['report'];
$startdt=$_GET['start'];
$enddt=$_GET['end'];
if ($type=="cumulative" and $enddt!=''){
$query="SELECT * from sae_cum_decoded('$enddt')";
$filename='SAE_CumulativeReport_';
$insideTitle="Cumulative SAEs Report";
processFile($db,$filename, $insideTitle,$query);
}
elseif($type=='new' and $startdt!='' and $enddt!=''){
$query="SELECT * from sae_new_decoded('$startdt','$enddt')";
$filename='SAE_NewReport_';
$insideTitle="New SAEs Report";
processFile($db,$filename, $insideTitle,$query);
}
else
echo '<script language="javascript">';
echo 'alert("In order to generate a report, a REPORT TYPE needs to be selected.\nFor Cumulative SAEs Report, END DATE is required.\nFor New SAEs Report, START DATE and END DATE are required.")
window.location.href="saereport5.php"';
echo '</script>';
}
function processFile($db,$filename, $insideTitle, $query){
...}
最好弄清楚如何使用这些按钮来执行此操作,因为将来用户将需要同时拥有这两个选项。