Jay
Jay

Reputation: 782

URL special char as GET method issue

Unfortunately htmlentities, urlencode, htmlspecialchars - nothing seems to work for me. I am trying to send the string "Music & Dance" in the URL as GET params, like this:

http://www.example.com?myvar=str.....

When I try to receive myvar, it always shows "Music". What this means is, the string gets cut off at the & char.

Any help would be highly appreciated.

Upvotes: 0

Views: 79

Answers (2)

unor
unor

Reputation: 96507

STD 66 defines the & as reserved character. It is typically used to delimit name-value pairs.

As your & is part of the value, you need to percent-encode it → %26. So your value should be Music%20%26%20Dance (%20 for space).

Upvotes: 0

sisve
sisve

Reputation: 19781

The result should be ?myvar=Music%20%26%20Dance.

Check that you both url-encode the values, and html-encode the complete url. The later is needed to make valid html.

Example:

var value1 = "Music & Dance";
var value2 = "Awesomeness";
var qs = "?myvar=" + urlencode(value1) + "&level=" + urlencode(value2);
// Result: "?myvar=Music%20%26%20Dance&level=Awesomeness"

var href = htmlencode(qs);
// Result: "?myvar=Music%20%26%20Dance&level=Awesomeness";

write("<a href=\"" + href + "\">my link!</a>");

Upvotes: 1

Related Questions