开发者

Preserve $_POST variables through different pages like $_SESSION

开发者 https://www.devze.com 2023-01-10 22:30 出处:网络
Once some data 开发者_如何学编程are submitted through POST, is it possible to make them available as $_POST through different pages, same like how $_SESSION allows us to do?You\'ll need to parse the d

Once some data 开发者_如何学编程are submitted through POST, is it possible to make them available as $_POST through different pages, same like how $_SESSION allows us to do?


You'll need to parse the data in $_POST and recreate it in your form. You can do it with hidden fields.

Or, you can save the $_POST data in a user session and refer to it when you need it. You'll have to manage the lifecycle of the data to make sure it doesn't stay around too long.


Digging up an old question today. But i forgot to post the working solution I dug....

Place this snippet in top of your every page

if(isset($_POST) & count($_POST)) { $_SESSION['post'] = $_POST; }
if(isset($_SESSION['post']) && count($_SESSION['post'])) { $_POST = $_SESSION['post']; }


The problem is that you are sending the data in via the URL which is stored in the $_GET variable, not in $_POST. If you want the ability to submit the data in either format, use $_REQUEST instead.

There is a bit of a debate on whether it is a good idea to use $_REQUEST, but if you are doing a simple site, there is little wrong with it.

If you would rather not use $_REQUEST, then you can use the following code on each variable you are expecting:

if (!empty($_GET['foo'])) {
    $foo = $_GET['foo'];
} elseif (!empty($_POST['foo'])) {
    $foo = $_POST['foo'];
} else {
    die("Foo not submitted");
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号