Squirrel
Squirrel

Reputation: 15

How to pass a hashtag in a ajax request

I have a chat that uses ajax to send messages to the server. My problem is that when the user tries to send a hashtag, everything after the hashtag is omitted. I saw a similar question like this that said encoding would help. I tried encoding it, but it created bigger problems when it got to the php. It started counting the + sign as a space and made the first two numbers after a % sign go missing. Is there a workaround to this in php, or should I try something other than encoding.

here is the relevant javascript with the encoding.

ajax.open("GET",   "/chatAdd.php?msg=" + encodeURIComponent(message.value) +   "&user=" + encodeURIComponent(user) + "&id=" + encodeURIComponent(id));

And the php i used to decode it.

$user = urldecode($_REQUEST["user"]);
$userID = urldecode($_REQUEST["id"]);
$msg = urldecode($_REQUEST["msg"]);

Upvotes: 1

Views: 1482

Answers (1)

Munsterlander
Munsterlander

Reputation: 1386

Use Percent encoding. Replace the hash with %23.

Edit: Also, a really great resource for escaping in JS and PHP - http://www.the-art-of-web.com/javascript/escape/

Upvotes: 2

Related Questions