如何使用会话将值从一个 php 页面传递到另一个页面 [英] how to pass value from one php page to another using session
问题描述
我可以将值从一个页面传递到另一个页面,但我需要像这样传递值,
I can pass values form one page to another but I need to pass value like this,
第 1 页:
Page4.php
Page3.php
我需要将Page1.php中的文本字段中的值传递给Page2.php中的文本字段,因为表单没有直接重定向到page2,我无法传递该值,我试过session,formpost方法和其他一些方法,但我还没有成功.
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
<?php
session_start();
$_SESSION['myValue']=3; // You can set the value however you like.
?>
任何其他 PHP 页面:
Any other PHP page:
<?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.";
}
这篇关于如何使用会话将值从一个 php 页面传递到另一个页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!