gpt4 book ai didi

php - 将 session 变量从 html 表单传递到多个 php 页面 url?

转载 作者:行者123 更新时间:2023-11-30 00:15:58 26 4
gpt4 key购买 nike

我正在尝试将变量从表单传递到 php 中的多个页面

表单“rentcheck.php”

<?php require ("Connections/Project.php") ;?>
<?php session_start();?>

<title>Rent Check</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="rent.php">
<table width="385" height="70" border="1">
<tr>
<td><label for="select3">Select Customer</label>
<select name="Customer_ID" id="Customer_ID">
<?php
//Select from SQL Database Table (t_customer)

$sql=mysql_query("SELECT * from t_customer");

while ($Customer = mysql_fetch_array($sql)){
echo "<option value='".$Customer['Customer_ID']."'>".$Customer['Customer_Name']."</option>";
}
?>
</select></td>
</tr>
</table>
<p>
<input type="submit" name="button" id="button" value="Submit" />
</p>
</form>

</body>
</html>

输入后传递到页面“rent.php”

<?php
require("Connections/Project.php");


//$_SESSION['yourvariable'] = 'foo';

//$newDate = date("d-m-Y", strtotime($row_Recordset1['Customer_CC_Exp_Date']));
session_start();
$datetoday=date("Y-m-d H:i:s");
$endOfCycle=date('Y-m-d', strtotime("+30 days"));
if(isset($_GET['page'])){


$pages=array("products","cart");

if(in_array($_GET['page'], $pages)){

$_page=$_GET['page'];


}else{
$_page="products";
}

}else{
$_page="products";

}
?>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Rent</title>
</head>
<body>
<p> <?php require($_page. ".php");?>
<?php echo $_POST['Customer_ID'];?></p>
</body>
</html>

此页面 (rent.php) 显示表单中的值。

第三页“products.php”

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>



<?php

if(isset($_GET['action']) && $_GET['action']=="add"){


$custget=$_SESSION['Customer_ID'];
$id=intval($_GET['id']);

if(isset($_SESSION['cart'][$id][$custget])){

$_SESSION['cart'][$id]['quantity']++;
$getcust=$_SESSION['Customer_ID'];

}else{
$sql_s="SELECT * FROM t_dvd_copy
WHERE dvd_copy_id={$id}";
$query_s=mysql_query($sql_s);
if(mysql_num_rows($query_s)!=0){
$row_s=mysql_fetch_array($query_s);

$_SESSION['cart'][$row_s['dvd_copy_id']]=array(
"quantity" =>1,
"price" => $row_s['price']
);


}else{
$message="NO";

}
}
}
?>

<?php if(isset($message)) {
echo"$message"; }
//echo print_r($_SESSION['cart']); ?>

<table width="489" height="52" border="1">
<tr>
<td width="123">DVD Copy ID</td>
<td width="120">Name</td>
<td width="91">Price</td>
<td width="127">Action</td>
</tr>


<?php
$sql="SELECT *, dvd_title FROM t_dvd_copy INNER JOIN t_dvd ORDER BY dvd_title ASC";
$query=mysql_query($sql);

while($row=mysql_fetch_array($query)) {

?>

<tr>
<td><?php echo $row['dvd_copy_id']?></td>
<td><?php echo $row['dvd_title']?></td>
<td><?php echo $row['price']?></td>
<td><a href="rent.php?page=products&action=add&id=<?php echo $row['dvd_copy_id']?>">Add To Cart</a></td>
<?php
}
?>

</table>

<body>
</body>
</html>

此页面(products.php)显示:

Notice: Undefined index: Customer_ID in C:\xampp\htdocs\project3\rent.php on line 39" whenever I clicked the "Add to Cart" or manually type "rent.php?=cart".

我想做的是在多个页面上显示(Customer_ID)/传递变量(“products.php”,“cart.php”)。

有什么建议或想法吗?

最佳答案

我认为您的问题是您尚未在其他页面上启动 session 。在您想要建立 session 的每个 php 页面上,您需要将 session_start(); 放在顶部。

如果您不这样做, session 将结束并清空其中的所有数据。

如果您想确定 session 中有什么内容,您可以像这样打印出来:

echo "<pre>";
echo print_r($_SESSION);
echo "</pre>";

关于php - 将 session 变量从 html 表单传递到多个 php 页面 url?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23659205/

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