0

我正在尝试使用 SESSION 将一些变量从一个 PHP 页面传递到下一个页面。

第一个页面包含此代码,用户单击提交后,它转到第 2 页:

<!--SESSIONS TO PASS ON VARIABLES-->
<?php $_SESSION['file'] = $file; ?>
<?php $_SESSION['linecount'] = $linecount; ?>
<?php $_SESSION['priceperpost'] = $priceperpost; ?>
<?php $_SESSION['totalcost'] = $totalcost; ?>
<!--//SESSIONS TO PASS ON VARIABLES-->

这就是我在提交表单后在下一页第 2 页上的内容:

<!--VARIABLES FROM PREVIOUS PAGE-->
<?php $file = $_SESSION['file']; ?>
<?php $linecount = $_SESSION['linecount']; ?>
<?php $priceperpost = $_SESSION['priceperpost']; ?>
<?php $totalcost = $_SESSION['totalcost']; ?>
<!--//VARIABLES FROM PREVIOUS PAGE-->  

但它没有得到代码,因为我试图在第 2 页上回显变量:

  <u>Number of Posts:</u> <?php echo ($linecount); ?><br>
  <br>
  <u>Price Per Post:</u> $<?php echo ($priceperpost); ?> <br>
  <u>Total Cost:</u> $<?php echo ($totalcost); ?><br>
  <br>

请帮助,因为我不知道我做错了什么,任何帮助将不胜感激。

是的,我也调用了 session_start

这是更新的代码:

 <?php 
session_start(); 
$_SESSION['file'] = $file;
$_SESSION['linecount'] = $linecount;
$_SESSION['priceperpost'] = $priceperpost;
$_SESSION['totalcost'] = $totalcost; 
?>

在下一页

<?php
session_start();
$file = $_SESSION['file'];
$linecount = $_SESSION['linecount'];
$priceperpost = $_SESSION['priceperpost'];
$totalcost = $_SESSION['totalcost']; 
?>
4

2 回答 2

0

您确定您的变量已设置,即 $totalcost 吗?对您的代码的评论。除非,您正在进出 PHP 和 HTML,否则您不需要在每一行都打开和关闭 PHP。

这可以重写:

<!--SESSIONS TO PASS ON VARIABLES-->
<?php $_SESSION['file'] = $file; ?>
<?php $_SESSION['linecount'] = $linecount; ?>
<?php $_SESSION['priceperpost'] = $priceperpost; ?>
<?php $_SESSION['totalcost'] = $totalcost; ?>
<!--//SESSIONS TO PASS ON VARIABLES-->

作为:

<?php
// SESSIONS TO PASS ON VARIABLES
session_start();
$_SESSION['file'] = $file;
$_SESSION['linecount'] = $linecount;
$_SESSION['priceperpost'] = $priceperpost;
$_SESSION['totalcost'] = $totalcost; 
// SESSIONS TO PASS ON VARIABLES
?>
于 2013-01-03T21:25:51.423 回答
0

确保在创建变量之前和回调它们之前开始会话:

session_start();
于 2013-01-03T21:17:26.107 回答