gpt4 book ai didi

php - session 未将 POSTed 数据传递给第二个脚本 - 请告知

转载 作者:行者123 更新时间:2023-11-29 12:05:38 24 4
gpt4 key购买 nike

尝试询问此问题,它被标记为重复并带有建议。我采用了这些建议,但似乎仍然没有达到预期的结果。有人能真正阅读以下内容并告诉我哪里出错了吗?

我正在尝试使用 session 将用户选择的数据从此脚本(allotment.php)传递到后续脚本(allotmentreport.php),其中它在查询限定符中使用(例如...WHERE tablecolumndata = session变量...)。选择选项并单击“提交”后,我没有从 allotment.php 收到错误,但数据无法传递到 allotmentreport.php 并返回 undefined variable 的错误。

这行和它后面的行正确吗?否则我还缺少什么吗?$tourselect=(isset($_POST['提交']));

更新最终和更正的代码如下所示,供 future 用户寻求工作示例和易于阅读的解决方案:

    <?php 
session_start();
$host="localhost";
$username="HIDDEN";
$password="HIDDEN";
$dbname="bookings2015";
$con = mysql_connect($host, $username, $password, $dbname);
if (!$con)
{
die ('damn thing wont connect to the MYSQL server: Maybe it is retarded '. mysql_error());
}
mysql_select_db($dbname, $con);
?>
<!Doctype html>
<html>
<?php include 'C:\xampp\htdocs\phpproject1\head.php';
include 'config\menu.php';
?>
<div id="dataentry">
<div id="submit">
<?php
echo "Which Tour to check availability?&nbsp;&nbsp;&nbsp;";?>
<br />
<br />
</br>
</br>
<form method="post" action="allotment_report.php">
<select name='TourCode'>
<?php
$tourselection = "SELECT DISTINCT TourCode FROM toursanddates ORDER BY TourCode";
$result = mysql_query($tourselection);
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['TourCode'] . "'>" . $row['TourCode'] . "</option>";
}
?>
</select>
<input type="submit" name="tourselected" value="submit">
</form>
<?php
?>
</div>
</div>
<div id="demographicborder">
<?php
include 'footer.php';?>
</div>
</div>
</body>
</html>
</form>
</form>
</div>
</div>
</div>
</body>
</html>

这是allotment_report.php代码

        <?php 
session_start();
$host="localhost";
$username="STILLHIDDEN";
$password="STILLHIDDEN";
$dbname="bookings2015";
$con = mysql_connect($host, $username, $password, $dbname);
if (!$con)
{
die ('damn thing wont connect to the MYSQL server: Maybe it is retarded '. mysql_error());
}
mysql_select_db($dbname, $con);
include 'C:\xampp\htdocs\phpproject1\head.php';
include 'config\menu.php';
?>
<br />
<br />
<?php

//Table Header:
echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong><u>Tour Availability</u>";
echo '<table align="center" cellspacing="3" cellpadding="3" width="75%">
<tr>
<td align=:"left"><b>Tour:</b></td>
<td align=:"left"><b>Start Date:</b></td>
<td align=:"left"><b>Seats Avail:</b></td>
<td align=:"left"><b>Rooms Avail:</b></td>
</tr>
';
if(isset($_POST['TourCode'])){
$tour=$_POST['TourCode'];
}
$status="ok";
$ar="SELECT TourCode, DATE_FORMAT (TourStart, '%m%d%y') AS TourStart, SeatsAvail, RoomsAvail FROM toursanddates WHERE TourCode='$tour' AND Status='$status' ORDER BY TourCode, TourStart ASC";
$result=mysql_query($ar);
$num_results = mysql_num_rows($result);
while($row = mysql_fetch_assoc($result)){
//Display the allotments fetched in above query
echo '<tr>
<td align=:"left">' . $row['TourCode'] . '</td>
<td align=:"left">' . $row['TourStart'] . '</td>
<td align=:"left">' . $row['SeatsAvail'] . '</td>
<td align=:"left">' . $row['RoomsAvail'] . '</td>
</tr>
';
}
echo '</table>';
//echo "</strong>Tour:&nbsp;&nbsp;".($row['TourCode']);
//echo "</strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Start Date: ".($row['TourStart']);
?>
<br />
<?php
echo "<br />";
echo "</p>";
?>
</br>
</br>
</div>
</form>
</div>
<div id="demographicborder">
<?php include 'footer.php';
?>
</div>
</div>
</body>
</html>
</form>
</form>
</div>
</div>
</div>
</body>
</div>
</body>

最佳答案

<?php
//contained within allotment.php
$tourselect=(isset($_POST['tourselected']));
$_SESSION['tourselected']=$tourselect;
?>

看起来您希望在用户第一次加载打开页面时在allotment.php 上设置$_SESSION['tourselected']。然而,这种情况并非如此。 $_POST 数据附加在 HTTP 请求中。当您第一次加载 alloment.php 时,浏览器不会向其发送任何 $_POST 数据。这可以解释为什么当您进入第二个脚本时 $_SESSION['tourselected'] 未设置。

也就是说,如果您的唯一目标是将数据从 alloment.php 中内置的表单发送到 alloment_report.php 您根本不应该使用 session 。 所有这些都可以通过仅$_POST

考虑以下代码:

 <!--alloment.php-->
<form method="post" action="alloment_report.php">
<select name='TourCode'>
<?php
//Assume that $options contains stuff pulled from your database.
foreach($options as $option) {
echo "<option value='" . $option . "'>" . $option . "</option>";
}
?>
</select>
<input type="submit" name="tourselected" value="submit">
</form>

当用户填写表单并单击“提交”时,alloment_report.php(由 action="alloment_report" 指定)获取通过 $_POST 从表单发送的数据(由 method="post" 指定)。

<!--alloment_report.php-->
<?php
if(isset($_POST['tourcode'])){
echo "yay! the tour has been selected!";
}
?>

关于php - session 未将 POSTed 数据传递给第二个脚本 - 请告知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31527588/

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