简体   繁体   English

在PHP中POST并将用户重定向到该页面?

[英]POST in PHP AND redirect the user to that page?

I'm a bit confused. 我有点困惑。 How do I POST data to a URL and then redirect the user's browser to that location - all in one operation? 如何将数据发布到URL,然后将用户的浏览器重定向到该位置 - 所有操作都在一个操作中?

I see 我懂了

header('Location: page.php?' . http_build_query($_POST)); 

but that is GET, not POST and ppl think thats really bad practice - PHP open another webpage with POST data (why?) 但这是GET,而不是POST和ppl认为这真的是不好的做法 - PHP用POST数据打开另一个网页 (为什么?)

My kludgy workflow involves setting up a form and then submitting it via javascript - anything has to be better than that... 我的kludgy工作流程涉及设置一个表单,然后通过javascript提交 - 任何东西都必须比那更好......

I think I can do a set of header() stmts but this action happens for the user way after the page has been geenrated, so i dont think that would work 我想我可以做一组header()stmts,但是这个动作是在页面被生成后以用户的方式发生的,所以我不认为这会起作用

You cannot redirect POST requests. 无法重定向POST请求。 As simple as that. 就如此容易。 Any redirect will always turn into a GET request. 任何重定向都将始终变为GET请求。

If you want to receive POST data, then send that data to another page, you have two choices: 如果您想接收POST数据,然后将该数据发送到另一个页面,您有两个选择:

  • if both pages are on the same server, use sessions to save the data server-side, don't make the client carry it over 如果两个页面都在同一台服务器上,则使用会话保存数据服务器端,不要让客户端将其携带
  • if the destination is on another server and you need to send the client there together with the data, set up another intermediate form like you are 如果目标位于另一台服务器上,并且您需要将客户端与数据一起发送到那里,请设置另一个中间表单,就像您一样

Use AJAX to save the data before you leave the page. 在离开页面之前使用AJAX保存数据。 Use the answer you get back to fire a redirection to the new url right within the current page. 使用您获得的答案在当前页面内向右侧激发重定向。 Don't be affraid of Javascript and ajax. 不要害怕Javascript和ajax。 Try this light AJAX library: http://www.openjs.com/scripts/jx/ 试试这个轻量级AJAX库: http//www.openjs.com/scripts/jx/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM