将 PHP 变量从一个动态页面传递到另一个 [英] Passing PHP Variable From One Dynamic Page to Another

查看:30
本文介绍了将 PHP 变量从一个动态页面传递到另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to pass on some variables using SESSION from one PHP page to the next.

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

The first page contains this code and after the user clicks submit, it goes to Page 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 页)上的内容:

And this is what I have on the next page, Page 2, after the form is submitted:

<!--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 页上回显变量:

But it is not getting the code because I am trying to echo the variables on 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>

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

Please help as I do not know what I am doing wrong, any help would be greatly appreciated.

是的,我也调用了 session_start

Yes I have also called 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']; 
?>

推荐答案

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

Are you sure your variables are set i.e. $totalcost? A comment on your code. Unless, you are breaking in and out of PHP and HTML, you do not need to open and close PHP on every line.

这可以重写:

<!--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
?>

这篇关于将 PHP 变量从一个动态页面传递到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆