2014-02-14 19:24
浏览 44


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">

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/');

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


instead of the expected...


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.

    解决 无用
    打赏 举报

相关推荐 更多相似问题