Javascript 如何将变量从一个PHP脚本拉到另一个PHP脚本
无法将变量从一个PHP拉到另一个脚本 我有三个不同的文件:adminPage.html、reportScript.php和report.php html从用户获取变量,并使用AJAX post函数将变量发布到reportScript.phpJavascript 如何将变量从一个PHP脚本拉到另一个PHP脚本,javascript,php,Javascript,Php,无法将变量从一个PHP拉到另一个脚本 我有三个不同的文件:adminPage.html、reportScript.php和report.php html从用户获取变量,并使用AJAX post函数将变量发布到reportScript.php $sql = "SELECT * FROM orderlist WHERE NOT (dateOrdered < $startDate OR dateOrdered > $endDate)"; report.php应该从report
$sql = "SELECT * FROM orderlist WHERE NOT (dateOrdered < $startDate OR
dateOrdered > $endDate)";
report.php应该从reportScript.php中提取这些已发布的变量,并在SQL函数中使用这些变量,但是,我收到一个错误,指出我有一个未定义的索引:startDate和未定义的索引:endDate,我正在php中实例化这些变量
adminPage.html:
<center><h2> Choose the dates below that you need an order list from: </h2>
</br>
<form>
<h2>Start:</h2>
<input type="date" id ="reportStartDate" name = "startDate">
</br>
<h2>End:</h2>
<input type="date" id ="reportEndDate" name = "endDate">
</form>
</center>
</br></br>
<button id="runReportButton" onclick = "runReport()"> Run Report </button>
<script>
function runReport()
{
var jStartDate;
var jEndDate;
jStartDate = document.getElementById("reportStartDate").value;
jEndDate = document.getElementById("reportEndDate").value;
/*console.log(jStartDate);
console.log(jEndDate); */
$.ajax
({
type: "POST",
url: "phpScripts/reportScript.php",
data: {startDate: jStartDate, endDate: jEndDate},
success: function(response)
{
console.log("posted");
window.open("report.php", "_self");
}
});
}
</script>
reportScript.php:
<?php
require 'connect.php';
//posts data to db
$startDate = $_POST["startDate"];
$endDate = $_POST["endDate"];
$sql = "SELECT * FROM orderlist WHERE NOT (dateOrdered < startDate OR
dateOrdered > endDate)";
$result = $conn->query($sql);
if($result){
echo "true";
}
else{
echo "false";
}
?>
report.php:
<?php
require 'phpScripts/connect.php';
require 'phpScripts/reportScript.php';
//posts data to db
/*$startDate = $_POST['startDate'];
$endDate = $_POST['endDate'];*/
/*$startDate = '2018-01-01';
$endDate = '2018-08-08'; */
$sql = "SELECT * FROM orderlist WHERE NOT (dateOrdered < '$startDate' OR dateOrdered > '$endDate');";
$result = $conn->query($sql);
//above is reportScript.php, below is pulling list method from order.php
//below works, just needs variables from the reportScript
echo "<ul>";
if($result->num_rows >0)
{
$i = 0;
while($row = $result->fetch_assoc()) // this loads database into list, also
creates array of pricing which someone can pull from later to get total
{
echo "<li style='font-size:15px'>".$row["drinkName"]. ", Date Ordered: "
.$row["dateOrdered"] . ",Cost: " .$row["drinkCost"] . "</li>";
echo "</br>";
$i = $i+1;
}
}else {
echo "<p> you're a dummy and you did this wrong </p>";
}
echo "</ol>";
?>
您在reportScript.php中的变量中忘记了美元符号$
$sql = "SELECT * FROM orderlist WHERE NOT (dateOrdered < $startDate OR
dateOrdered > $endDate)";
此语句也容易受到攻击。根据@Ralf的一些建议,我将reportScript.php和report.php合并在一起,并在打开时使用$\u GET语句将日期变量放入URL中。这样,查询就不会被放置两次,变量也会被保存。谢谢您的帮助!美元符号肯定是我错过的东西,但是它并没有解决问题,我仍然会出现同样的错误。我认为问题在于你的ajax中的window.open。您正在启动另一个线程,当您需要reportScript.php脚本时,$\u POST数组不再分配。也许您应该在ajax请求和php工作输出中完成所有必须完成的工作。非常感谢您的帮助。我结合了report.php和reportScripts.php,本质上是将AJAX函数直接发布到report.php。但是,我不确定如何在不启动报表的情况下打开report.php窗口,因此会出现相同的错误。