【问题标题】:How to redirect with a $_SESSION variable from a form如何使用表单中的 $_SESSION 变量进行重定向
【发布时间】:2012-12-02 00:06:55
【问题描述】:

我有点缺乏经验,所以请放轻松。

我需要在提交表单之前保存表单文本区域中的值(即使在页面重新加载后我也需要它)。

重新加载后,我需要重定向到网站上的预定义页面,该页面在 URL 的最后包含 textarea 值。

到目前为止,我有这样的事情:

<php?
session_start();
$_SESSION['textarea_value'] = $_POST['textarea_name'];
?>

// below is called directly after a popup form submission
location.reload();
if ($_SESSION['textarea_value'] != null) {
    header("Location: http://www.xxxxxxxx.com/?s=$_SESSION['textarea_value']");
    unset($_SESSION['textarea_value']);
}

【问题讨论】:

  • www.xxxxxxxx.com 与设置会话的站点是否相同?
  • 附带说明,您需要在$_SESSION['textarea_value'] 上致电urlencode,否则您可能会遇到网址问题
  • (a) location.reload(); 在做什么?那是JavaScript吗? (b) 您是否尝试将die() 放入 if 语句中? (c) 你在问什么?有什么问题吗?

标签: php session variables redirect


【解决方案1】:

好的,这就是我要做的:

方法#1

index.php

<?php 
    if($_POST)
    {
        session_start();
        $_SESSION["someVar"] = $_POST["someVar"];
        header("Location:otherPage.php");
    }
?>

<form action="" method="post">
<textarea name="someVar"></textarea><br/>
<input type="submit" value="submit">
</form>

otherPage.php

<?php

    session_start();
    $someVar = $_SESSION["someVar"];

?>

方法#2:如果你想用get请求来做,你甚至不需要使用会话:

index.php

<?php 
    if($_POST)
    {
        header("Location:otherPage.php?someVar=".$_POST["someVar"]);
    }
?>

<form action="" method="post">
<textarea name="someVar"></textarea><br/>
<input type="submit" value="submit">
</form>

otherPage.php

<?php

    $someVar = $_GET["someVar"];

?>

方法#3:你甚至可以完全去掉重定向:

index.php

<form action="otherPage.php" method="post">
<textarea name="someVar"></textarea><br/>
<input type="submit" value="submit">
</form>

otherPage.php

<?php

    $someVar = $_POST["someVar"];

?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-14
    • 1970-01-01
    • 2016-05-30
    • 1970-01-01
    相关资源
    最近更新 更多