Can I set a POST variable before redirecting to a

2019-08-12 19:33发布

Possible Duplicate:
How do you POST to a page using the PHP header() function?

For example, I need to redirect a user to a different page upon error, but I also want to POST a value to that page as well.

标签: php
4条回答
看我几分像从前
2楼-- · 2019-08-12 19:53

You definitely cannot POST and redirect in the same time. Redirect means setting several HTTP headers. However, there are several solutions to your needs.

One is to store the data into $_SESSION and retrieve it from there, as answered above.

Another way is to put the form aside from PHP, in a separate file and include it if you have errors in the data posted, so you have all the variables in the current POST without the need to redirect the user (and set an error message in the form too).

You can think to use an AJAX to make the post and get the result back then you'll do the redirection using javascript.

Or, if there aren't many variables/values you want to pass you may use GET when doing the redirection.

I'm sure other alternatives exists too.

查看更多
倾城 Initia
3楼-- · 2019-08-12 19:55

Could you use $_SESSION instead?

$_SESSION['var'] = 'foo';
//redirect
查看更多
smile是对你的礼貌
4楼-- · 2019-08-12 20:03

When a redirect is performed, a GET request is made. I don't believe it's possible to make a POST redirect.

You could modify your code to check for both POST and GET requests by using the $_REQUEST super global variable. That way, you can pass whatever parameters you need in the redirect as GET parameters and still have your PHP script pick them up.

But, if you are working with a multi-step form, it might be a good idea to use session variables. This allows you to save the user's entered information from each step in the session, making it easy to load it again if they go back to a previous step.

查看更多
闹够了就滚
5楼-- · 2019-08-12 20:09

In the action="" html tag just put the address you are redirecting them to. This will post the value to that page as well.

查看更多
登录 后发表回答