gpt4 book ai didi

javascript - 如何将变量从一个 PHP 脚本拉到另一个 PHP 脚本

转载 作者:行者123 更新时间:2023-12-03 01:40:07 24 4
gpt4 key购买 nike

将变量从一个 PHP 提取到另一个脚本时遇到问题。

我有三个不同的文件:adminPage.html、reportScript.php 和report.php。

adminPage.html 从用户处获取变量并使用 AJAX post 函数将变量发布到 reportScript.php。

report.php 应该从 reportScript.php 中提取这些发布的变量并在 SQL 函数中使用这些变量,但是,我收到一条错误,指出我有“ undefined index :startDate”和“ undefined index :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";
}
?>

报告.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)";

此语句也容易受到 sql injection 的攻击.

关于javascript - 如何将变量从一个 PHP 脚本拉到另一个 PHP 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50884466/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com