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 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了