如何通过 $_GET 接受 URL 中的哈希标记?
据我所知,井号 (#) 不会发送到服务器,因此我似乎无法使用原始 PHP 来解析数据,如下面的 URL 所示
index.php?name=Ben&address=101 S 10th St Suite #301
:希望使用此 $_GET 数据预先填充表单字段。我将如何使用 Javascript(或 jQuery)来做到这一点,对于不使用 Javascript 的人来说,是否有一个不会破坏我的表单的后备方案?目前,如果存在哈希值(通常在地址字段中),则其后的所有内容都不会在 $_GET 中解析或存储。
From what I have been able to understand, hash marks (#) aren't sent to the server, so it doesn't seem likely that I will be able to use raw PHP to parse data like in the URL below:
index.php?name=Ben&address=101 S 10th St Suite #301
I'm looking to pre-populate form fields with this $_GET data. How would I do this with Javascript (or jQuery), and is there a fallback that wouldn't break my form for people not using Javascript? Currently if there is a hash (usually in the address field), everything after that is not parsed in or stored in $_GET.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以按照
urlencode
(在 php 中)或encodeURIComponent
(在 JavaScript 中)的方式对哈希进行编码。“哈希”不是请求的一部分,这就是您的脚本永远看不到它的原因。
You can encode the hash as you should
urlencode
(in php) orencodeURIComponent
(in JavaScript).The "hash" is not part of the request, which is why your script never sees it.
就像 webdestroya 所说,您需要发送带有 URL 的请求,
如果您使用 HTML 表单,那么当您提交表单时,字符串值将自动进行 urlencoded。
Like webdestroya said, you'll need to send a request with the URL
If you're using HTML forms, then the string value will be auto-urlencoded when you submit the form.
将链接粘贴到电子邮件中之前,您需要将链接编码为 Ben 引用的内容。您当前拥有的根本不是 URL。
您可以选择在查询参数上下文中将空格编码为
+
而不是%20
,但绝对不能包含原始空格,因为它是 URL 的定义特征,如果您在 Web 浏览器中在 URL 中键入空格,它会悄悄地修复错误,但如果电子邮件客户端充满空格,则无法从纯文本中挑选出 URL。有时有一个替代函数将空格编码为
+
而不是%20
。通常最好避免这种情况,因为+
并非在所有情况下都有效,但如果愿意:那么您可以使用 PHP 的
urlencode
函数,而不是更标准的rawurlencode
。无论哪种方式,您必须将哈希编码为
%23
,因为否则HTTP URL中的哈希意味着片段标识符(浏览器滚动到的页面部分) 。这不是页面本身地址的一部分;它甚至没有从浏览器传递到服务器,因此您当然无法从$_GET
或任何其他接口检索它。组件中还有许多其他字符(例如地址),在插入 URL 字符串之前必须对其进行 % 编码,否则它们会给您留下无效或无法正常工作的 URL。如果 URL 中的所有
%23
业务看起来很有趣……那么,您就必须忍受它。这就是 URL 一直以来的样子。You need to encode the link to what Ben quoted before you stick it in the e-mail. What you currently have is not a URL at all.
You can optionally encode a space to
+
instead of%20
in the context of query parameters but you absolutely cannot include a raw space, because it is a defining characteristic of URLs that they don't have spaces in. If you type a space in a URL in a web browser it will quietly fix up the mistake, but an e-mail client can't pick out a URL from plain text if it's full of spaces.There is sometimes an alternative function which encodes spaces to
+
instead of%20
. Normally this is best avoided as+
isn't valid in all circumstances, but if prefer:then you'd use PHP's
urlencode
function instead of the more standardrawurlencode
.Either way, you must encode the hash to
%23
, because otherwise a hash in an HTTP URL means the fragment identifier (the part of the page to scroll the browser to). This is not part of the address of the page itself; it is not even passed from the browser to the server, so you certainly cannot retrieve it—from$_GET
or any other interface.There are many other characters in a component like an address that must be %-encoded before being inserted into a URL string, or they'll leave you with an invalid or otherwise non-functional URL. If all that
%23
business looks funny in a URL... well, you'll have to live with it. That's what URLs have always looked like.我通常将哈希值存储在 onunload ej 的 cookie 上
:
I usually store the hash on a cookie onunload
ej: