我怎样才能得到文本框的值从一个页面到几个页面


How can I get the value of the textbox from one page to several pages?

如何从一个页面获取textbox的值到几个页面呢?让我们以以下示例为例:

<form action = "next.php" method = "post">
    <input type = "text" name = "txtname">
    <input type = "submit" name = "btnSubmit">
</form>

如果希望自动获得next.php上命名为txtname的文本框的值,我肯定可以得到它,因为动作是next.php,但如果我想在其他php文件上看到它怎么办?

您可以:

将其存储在next.php的会话变量中:

session_start();
$_SESSION['txtname'] = $_POST['txtname'];

您可以在其他页面中使用此值,只需在页面开头调用session_start(),然后

echo "Text Name: ".$_SESSION['txtname'];
  • 通过$_GET参数沿着页面传递它。
  • 使用cookie在本地存储变量,并在需要时检索它。

方法1:使用会话

next.php

if(isset($_POST['txtname']))
{
     $_SESSION['txtname'] = $_POST['txtname'];
}

anyother.php

if(isset($_SESSION['txtname']))
{
  echo $_SESSION['txtname'];
}

注意:在要访问会话的页面顶部使用session_start();



方法2:使用Cookie

next.php

if(isset($_POST['txtname']))
{
    set_cookie("txtname", $_POST['txtname']);
}

anyother.php

if(isset($_COOKIE['txtname']))
{
  echo $_COOKIE['txtname'];
}