dongzao9044 2014-02-14 19:24
浏览 44
已采纳

PHP标头位置在第二次重定向期间添加当前锚点

I'm submitting a form to itself with an anchor added to the URL so that when there are errors, the form is guaranteed to be in the viewport, here's a simplified example...

<form id="my_form" action="/form_link/#my_form" method="POST">
...
</form>

When /form_link/ receives the form post, it processes it, when there are errors it just continues loading the page again and shows the errors.

However when the post is successful it redirects to a different page using...

header('Location: /another_link/');
die;

The problem is that when I end up at the redirected link, the URL is actually...

/another_link/#my_form

instead of the expected...

/another_link/

I'm at a loss, the anchor is being added after the fact, so I have nothing to strip it from. How can I prevent this behaviour?


EDIT: Could the reason possibly be because nothing has yet been output to the browser between the form submit and the redirection? Is there anyway to clear whatever is in the header that's maintaining the anchor target?


EDIT 2: The headers that have already been sent as reported by headers_sent() are:

[0] => X-Powered-By: ZendServer 6.3.0
[1] => Set-Cookie: ZDEDebuggerPresent=php,phtml,php3; path=/
[2] => Expires: Thu, 19 Nov 1981 08:52:00 GMT
[3] => Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
[4] => Pragma: no-cache
[5] => Access-Control-Allow-Origin: **REMOVED**
[6] => Access-Control-Allow-Methods: GET, POST, OPTIONS
[7] => Access-Control-Allow-Headers: X-Requested-With

So I don't see anything in there that would affect follow up uses?!?!

  • 写回答

1条回答 默认 最新

  • duanniwu7730 2014-02-14 22:07
    关注

    The anchor is not sent to the server as part of the request; PHP has no knowledge of it. What you are seeing is client side (browser) behaviour. I just tested with other HTTP responses (301, 303, 304) and the result is the same.

    I would suggest using JavaScript to scroll down to the form in the event of an error, instead of relying on the URL.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog