如何使用会话将值从一个php页面传递到另一个
我可以将值从一页传递到另一页,但是我需要像这样传递值,
I can pass values form one page to another but I need to pass value like this,
第1页:
Page4.php
Page4.php
Page3.php
Page3.php
我需要将Page1.php的文本字段中的值传递给Page2.php的文本字段,因为该表单没有直接重定向到Page2,所以我无法传递该值,我尝试了会话,表单发布方法和其他几种方法,但我尚未成功.
I need to pass the value in a text field in the Page1.php to a text field in Page2.php, since the form is not directly redirectly to page2, I am unable to pass the value, I tried session, form post method and few other methods but I am yet to succeed.
如果您可以帮助我提供代码或一些建议,我将非常高兴.
I would be very happy if you can help me with the code or some suggestions.
谢谢!
编辑..........
Edit..........
我找到了答案,感谢您的帮助,这实际上是我的一个粗心的错误,我使用$ _post而不是$ _session.
I found the answer, thanks for the help, it was actually a careless mistake on my part, I used $_post instead of $_session.
它现在正在工作.
感谢您的帮助.
使用类似这样的内容:
page1.php
page1.php
<?php
session_start();
$_SESSION['myValue']=3; // You can set the value however you like.
?>
任何其他PHP页面:
<?php
session_start();
echo $_SESSION['myValue'];
?>
但是要记住一些注意事项:您需要在输出,HTML,回声-甚至空格之前调用session_start()
.
A few notes to keep in mind though: You need to call session_start()
BEFORE any output, HTML, echos - even whitespace.
您可以继续更改会话中的值-但只能在第一页之后使用 -这意味着,如果您在第1页中进行了设置,则将无法使用直到进入另一页面或刷新页面.
You can keep changing the value in the session - but it will only be able to be used after the first page - meaning if you set it in page 1, you will not be able to use it until you get to another page or refresh the page.
可以通过多种方式之一来设置变量本身:
The setting of the variable itself can be done in one of a number of ways:
$_SESSION['myValue']=1;
$_SESSION['myValue']=$var;
$_SESSION['myValue']=$_GET['YourFormElement'];
如果要在出现潜在错误之前检查变量是否已设置,请使用以下代码:
And if you want to check if the variable is set before getting a potential error, use something like this:
if(!empty($_SESSION['myValue'])
{
echo $_SESSION['myValue'];
}
else
{
echo "Session not set yet.";
}