How can I accept a hash mark in a URL via $_GET?

橙三吉。 提交于 2019-12-14 02:06:11

问题


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.


回答1:


You can encode the hash as you should urlencode(in php) or encodeURIComponent(in JavaScript).

The "hash" is not part of the request, which is why your script never sees it.




回答2:


Like webdestroya said, you'll need to send a request with the URL

index.php?name=Ben&address=101%20S%2010th%20St%20Suite%20%23301

If you're using HTML forms, then the string value will be auto-urlencoded when you submit the form.




回答3:


the user will be clicking a link from an email and will want to see the hash mark rendered in the email

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:

index.php?name=Ben&address=101+S+10th+St+Suite+%23301

then you'd use PHP's urlencode function instead of the more standard rawurlencode.

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.




回答4:


I usually store the hash on a cookie onunload

ej:

window.unload = function(){

 if(document.location.hash) setCoockie('myhash',document.location.hash);

};


来源:https://stackoverflow.com/questions/2954018/how-can-i-accept-a-hash-mark-in-a-url-via-get

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!