会话未将发布的数据传递给第二个脚本 - 请告知

Session not passing POSTed data to second script - please advise

试图询问这个问题,但它被标记为重复并带有一条建议。我采用的建议似乎仍然没有达到预期的效果。有人真的可以阅读以下内容并建议我哪里出错了吗?

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

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

更新 最终和更正后的代码显示在下面,供未来寻求工作示例和易于阅读解决方案的用户使用:

    <?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 ,您根本不应该使用会话。 所有这一切只需要 $_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(由 [=20= 指定)的表单发送的数据]).

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