将 PHP 变量从一个动态页面传递到另一个

编程入门 行业动态 更新时间:2024-10-23 15:19:09
本文介绍了将 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 变量从一个动态页面传递到另一个

本文发布于:2023-05-25 10:55:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/226621.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:词库加载错误:Could not find file &#039;D:\淘小白 高铁采集器win10\Configuration\Dict_Sto

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!