views:

157

answers:

4

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.

+2  A: 

You can encode the hash as you should urlencode(in php) or encodeURIComponents in Javascript.

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

webdestroya
So urlencode would auto encode the hash tag?
bccarlso
I don't believe this one works. I tried URL /test.php?name=Ben echo urlencode($get); and it gave me "BenSuite+ "
bccarlso
@bccarlso - No, you need to encode the URL before you send it. See how @Ben's answer is.
webdestroya
Gotcha, might not be possible due to this link being generated and sent out in an email, I can't do any encoding in their email, and would probably have a hard time encoding the emails before they get sent out. But now that I know what it would take, it gives me a better understanding, thanks.
bccarlso
@bccarlso - No problem. Yea you can't do much with the email, other than load a JS page that basically redirects them after encoding the URL
webdestroya
+1  A: 

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.

Ben Alpert
This might be difficult because the user will be clicking a link from an email and will want to see the hash mark rendered in the email, and not show up as %23 or %20, whichever it is. And I can't do any PHP processing in the email.
bccarlso
+1  A: 

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.

bobince
Thanks. This makes perfect sense to me and you guys have done a great job explaining it. The problem in my specific instance is that I will need to display "Suite #301" in both the email text that the user visibly sees, as well as "Suite #301" (No problem having %23 or Suite+%23301) in the URL, but I wouldn't want the user to see Suite+%23301 in the email. If I had a smart way to generate thousands of emails on the fly, I could probably do this, but I'm limited by that I think. (Unless there is a way to have HTML display HEX-encoded entities… and have it work in email clients.)
bccarlso
bobince
A: 

I usually store the hash on a cookie onunload

ej:

window.unload = function(){

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

};
Martin