在PHP中保持页面之间的会话?

use*_*309 2 php mysql forms session

我是PHP的新手.

我想在页面之间传输数据.

根据我的要求,我首先拥有主页,因为我有3个字段:名称,地址,引脚,然后提交按钮.

当我进入上面的字段,然后点击提交,它移动到page2.php,它有表格数据.

我已将第一个表单数据传输到第二页.现在在第二页我有一个提交按钮.当我点击该按钮时,数据将被提交到MySQL数据库.

我的问题是,如何将第一页值移动到insertdata.php页面并提交数据?

asp*_*rin 5

有两种方法可以做到这一点

  1. 会议
  2. 隐藏的输入字段

会议

要将数据从一个页面传递到另一个页面,首先需要调用session_start()将要使用$_SESSION超全局变量的所有页面.然后,您可以使用在会话中存储您的值

$_SESSION['name'] = $_POST['name'];
$_SESSION['address'] = $_POST['address'];
$_SESSION['pin'] = $_POST['pin'];
Run Code Online (Sandbox Code Playgroud)

要在第二页中使用这些值,只需按名称调用它们即可.例如:

$name = $_SESSION['name']; // will contain the value entered in first page
Run Code Online (Sandbox Code Playgroud)

================================================== ================================

隐藏的输入字段

这更像是一种乏味的方法,但它完成了这项工作.该过程涉及将需要传递的数据存储到隐藏字段中的不同页面,然后通过$_POST$_GET超全局访问它们.

page1.php(发布到page2.php)

<input type="text" value="Page 1 content" name="content" />
<input type="text" value="Page 1 body" name="body" />
Run Code Online (Sandbox Code Playgroud)

page2.php(发布到page3.php)

<input type="hidden" value="<?php echo $_POST['content']; ?>" name="content" />
<input type="hidden" value="<?php echo $_POST['body']; ?>" name="body" />
<input type="text" value="Page 2 content" name="content2" />
<input type="text" value="Page 2 body" name="body2" />
Run Code Online (Sandbox Code Playgroud)

page3.php

echo $_POST['content']; // prints "Page 1 content"
echo $_POST['body']; // prints "Page 1 body"
echo $_POST['content2']; // prints "Page 2 content"
echo $_POST['body2']; // prints "Page 2 body"
Run Code Online (Sandbox Code Playgroud)