简体   繁体   English

页面之间丢失表单数据

[英]Form data lost between pages

I am struggling to understand the session() command, even after reading numerous pages about it. 我正在努力理解session()命令,即使在阅读了很多关于它的页面之后。

I cannot get data from the form to appear on a subsequent page 我无法从表单中获取数据以显示在后续页面上

Here are two very simplified versions of my pages, can someone explain what is missing please 这是我的页面的两个非常简化的版本,有人可以解释缺少的东西


"submitform.php" ... “submitform.php”......

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Submit Form</title>
</head>

<body>

<?php
session_start();
$_SESSION['name']=$_POST['name'];
$_SESSION['email']=$_POST['email'];


if ($_SERVER["REQUEST_METHOD"] == "POST")
{
header('Location: displayform.php');
}


?>

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Name <input type="text" name="name"/><br/>
Email address <input type="text" name="email"/><br/>
<input class="submit" type="submit" name="submit" value="SUBMIT"/>
</form>

</body>

</html>

"displayform.php" ... “displayform.php”......

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Display Form</title>
</head>

<body>

<?php
session_start();
$_SESSION['name']=$_POST['name'];
$_SESSION['email']=$_POST['email'];

$name = $_SESSION['name'];
$email = $_SESSION['email'];

echo "Name: " . $name . "<br>";
echo "Email: " . $email;


?>

</body>

</html>

This code is a little strange, but I'm guessing it is just to understand sessions so here you go. 这段代码有点奇怪,但我猜这只是为了理解会话所以你走了。

As others have said, start the session at the top of each page. 正如其他人所说,在每个页面的顶部开始会话。 Once the form is submitted, store the values in session variables and redirect to the display page. 提交表单后,将值存储在会话变量中并重定向到显示页面。 There, you just display the session vars you saved on the first page. 在那里,您只需显示您在第一页上保存的会话变量。

submitform.php submitform.php

<?php
session_start();
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$_SESSION['name']=$_POST['name'];
$_SESSION['email']=$_POST['email'];
header('Location: displayform.php');
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Submit Form</title>
</head>

<body>

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Name <input type="text" name="name"/><br/>
Email address <input type="text" name="email"/><br/>
<input class="submit" type="submit" name="submit" value="SUBMIT"/>
</form>

</body>

</html>

displayform.php displayform.php

<?php
session_start();
$name = $_SESSION['name'];
$email = $_SESSION['email'];
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Display Form</title>
</head>

<body>
<?php
echo "Name: " . $name . "<br>";
echo "Email: " . $email;
?>
</body>

</html>

on 1st page you are setting $_SESSION['name'] = $_POST['name']; 在第一页上你设置$_SESSION['name'] = $_POST['name']; great. 大。

on page2 you are doing the same thing. 在第2页,你正在做同样的事情。 There is no POST['name'] on page 2. so $_SESSION['name'] is empty. 第2页没有POST['name'] 。所以$_SESSION['name']为空。

just echo $_SESSION['name'] on page 2 and enjoy. 只需在第2页上回显$_SESSION['name']享受。

You're duplicating $_SESSION['name']=$_POST['name']; 你复制$_SESSION['name']=$_POST['name']; on the second page. 在第二页。 The POST data is only available on the page the form submits to... so by the time the user reaches the second the page the values are empty. POST数据仅在表单提交的页面上可用...因此,当用户到达页面的第二页时,值为空。

Setting empty values to the session is the issue 将空值设置为会话是个问题

I have done few modification in your code as 我在你的代码中做了一些修改

"submitform.php" ... “submitform.php”......

<?php 
    session_start();// should be top of the page
 if(isset($_POST,$_POST['name'],$_POST['email'])){
    if ($_POST['name']!='' && $_POST['name'] !='')
    {   

      $_SESSION['name']=$_POST['name'];
      $_SESSION['email']=$_POST['email'];
        header('Location: displayform.php');
     }

 }

and "displayform.php" ... 和“displayform.php”......

 <?php
     session_start();

       $name = $_SESSION['name'];
      $email = $_SESSION['email'];

      echo "Name: " . $name . "<br>";
      echo "Email: " . $email;
  ?>

start the session @ the top of the page. 启动会话@页面顶部。

Change the submit form. 更改提交表单。 php file's form tag action as displayform.php. php文件的表单标记操作为displayform.php。

once the session is set you can just call again that specific session 设置会话后,您可以再次调用该特定会话

$name = $_SESSION['name']=$_POST['name'];
$email = $_SESSION['email']=$_POST['email'];

once the session is already started at the next page you can just call it 一旦会话已经在下一页开始,您就可以调用它

//simply like this
echo $name;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM