-1

我一直在阅读有关此的相关帖子,但我仍然没有得到正确的答案。我采纳了他们的建议,但未能实现我想要的。我想实现这一点:当我单击提交按钮时,我希望将 $_SESSION['roomno'] = 400 和 $_SESSION['chairnum']=5 传递到 PHP 中的另一个页面。

部分 monica.php 代码:

<div id="popupContact">
        <a id="popupContactClose">x</a>
        <center><form method = "POST" action="gervent.php">
        <h1>Information </h1>
        Fullname : <input type="text" name="student_name" /><br /><br />
        Student ID No. : <input type="text" name="stud_id" />
        <br /><br />
        <input type="submit"  value="Submit" onclick=???/>
        </form></center>
        </div>
        <div id="backgroundPopup"></div>

gervent.php

<?php
session_start();

$name = $_POST['student_name'];
$stud_id = $_POST['stud_id'];
/*Should have this
    $room_id = $_SESSION['roomno'];
    $chair_num = $_SESSION['chairnum'];
*/

mysql_connect("localhost", "root", "") or die (mysql_error());
mysql_select_db('seat_reservation') or die (mysql_error());

$query = mysql_query("INSERT INTO reservation WHERE (room_id, chair_number, chair_status, student_name, stud_id) VALUES ($room_id, $chair_num, 1, $name, $stud_id)");

if($insert)
    die ("Seat reserved! <br />Redirecting...<meta http-equiv='refresh' content=2;monica.php>");

else 
        die ("Try Again! <br />Redirecting...<meta http-equiv='refresh' content=2;monica.php>");
   ?>

所以 gervent.php 应该从 monica.php 获取 SESSION 值,以便它可以在插入时使用它。在点击中怎么可能?注意:简单解释一下,我对 PHP 快车道来说太慢了。谢谢!

4

2 回答 2

0

$_SESSION变量本身没有“通过” 。它们在脚本之间维护,因此不需要传递。您只需在需要时设置/更改它们,然后在服务器端的任何地方调用它们(前提是您session_start()在页面开头调用了,但我确定您已经知道了)。

于 2012-08-01T17:11:33.623 回答
0

虽然这没有意义,但只有在表单存在的页面中“专门”设置会话变量并且它们在其他页面中更改(我这么说是因为我猜这里的会话变量似乎是根据用户打开的房间页面设置,并且可能有多个房间页面要保留一个座位,每个页面设置不同的会话变量)或者如果会话是可选的并且在表单所在的页面中没有启动会话,那么您可以将隐藏的输入字段添加到具有房间值和椅子编号的表单中(并且您应该找到一种方法将它们预定义到表单中,例如 $_GET['value'] 等),然后您可以通过单击提交将它们发送到目标页面。

<div id="popupContact">
<a id="popupContactClose">x</a>
<center><form method = "POST" action="gervent.php">
<h1>Information </h1>
Fullname : <input type="text" name="student_name" /><br /><br />
Student ID No. : <input type="text" name="stud_id" />
<br /><br />
<!-- the php variables of values has to be set before, any way you'd like -->
<input type="hidden" name="room_id" value="<?=$room_id?>" />
<input type="hidden" name="chair_num" value="<?=$chair_num?>" />
<input type="submit" value="Submit" />
</form></center>
</div>
<div id="backgroundPopup"></div>

接收表单值的页面应该是这样的;

<?php
session_start();

$name = $_POST['student_name'];
$stud_id = $_POST['stud_id'];
$room_id = isset($_SESSION['roomno']) ? $_SESSION['roomno'] : $_POST['room_id']; // here you set the value according to an already present existance of a session or not
$chair_num = isset($_SESSION['chairnum']) ? $_SESSION['chairnum'] : $_POST['chair_num']; // the same as above but this time for the chair number

mysql_connect("localhost", "root", "") or die (mysql_error());
mysql_select_db('seat_reservation') or die (mysql_error());
// below sql query is corrected by changing the WHERE clause with COLUMNS
$query = mysql_query("INSERT INTO reservation COLUMNS (room_id, chair_number, chair_status, student_name, stud_id) VALUES ($room_id, $chair_num, 1, $name, $stud_id)");

if($query) //there was no $insert variable set. This condition would always return false because the sql query which you insert the values to the DB is assigned to $query, not $insert
    die ("Seat reserved! <br />Redirecting...<meta http-equiv='refresh' content=2;monica.php>");

else 
    die ("Try Again! <br />Redirecting...<meta http-equiv='refresh' content=2;monica.php>");
?>

并且没有必要以这种方式使用任何 javascript onclick 事件。

于 2012-08-01T18:11:26.740 回答