将值传递给另一个页面PHP

时间:2015-10-18 13:30:50

标签: php

if(isset($_POST['submit']) and $_POST['searcheditem'] != "") {
    $value = $_POST['searcheditem'];
    header("Location : anotherpage.php");
}

我在我的项目中使用此代码,但当我被重定向到anotherpage.php时,我无法使用$value。它不等于该页面的$_POST['searcheditem']

我该怎么办?

2 个答案:

答案 0 :(得分:3)

在这种情况下,您有两个选项:

1。使用查询字符串

$value = $_POST['searcheditem'];
header("Location : anotherpage.php?myValue=".$value);

// then inside anotherpage.php   
echo $_GET['myValue']; // make sure to sanitize this data

2。使用会话

$value = $_POST['searcheditem'];
$_SESSION['myValue'] = $value; // make sure to use session_start() at the top of the page
header("Location : anotherpage.php");

// then on the anotherpage.php page
// make sure you call session_start() at the top of this page too
echo $_SESSION['myValue']; // make sure to sanitize this too

答案 1 :(得分:1)

您可以通过两种方式完成此操作,可以在会话中设置它,也可以像查询字符串一样传递

查询字符串方法:

if(isset($_POST['submit']) and $_POST['searcheditem'] != "") {
    $value = $_POST['searcheditem'];
    header("Location : anotherpage.php?value=$value ");
}

会话方法:

if(isset($_POST['submit']) and $_POST['searcheditem'] != "") {
        $value = $_POST['searcheditem'];
        session_register("value"); // USE THIS ONLY IF YOUR PHP VERSION IS < 5.3.0
        $_SESSION["value"] = $value;
        header("Location : anotherpage.php");
    }