dongzao9044 2014-02-14 11: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 14: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.

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

报告相同问题?