/    Sign up×
Community /Pin to ProfileBookmark

How to handle ‘&’ as a param value in a get request url

Hi ,

In my webpage, I have a filtering mechanism to retrieve the tabular data as per the filter I used.

Let us assume that the webpage url is [url]http://myhost.com/homepage/employee/project[/url]

Now if I select a project name which has ‘&’ in it , for example ‘Design & Verification’ and press go , it calls the following Javascript function :

[CODE]function get_action (form_element) {
if ($(“#project”).val()) {
var str = $(“#project”).val();
$(“#filters”).attr(“action”, “/homepage/employee/project/” + str);
}
}[/CODE]

Here #filters is the form id and #project is the textbox id for that filter. Now if I write ‘Design & Verification’ in the textbox (#project) and press submit button,
in the above JS function str is having ‘Design & Verification’. And this & is not regarded as the value of the search string. The get request considers it used so as to separate the parameters passed and the search is failing.

How can I avoid this problem ?

Thanks.

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@nap0leonMay 13.2015 — In this case you are dealing with malformed QueryString parameters. They need to be encoded.

"value=design & verification"

should be

"value=design%20%26%20verification"
Copy linkTweet thisAlerts:
@iBeZiMay 13.2015 — And to do that encoding you can use encodeURIComponent

<i>
</i>function get_action (form_element) {
if ($("#project").val()) {
var str = $("#project").val();
$("#filters").attr("action", "/homepage/employee/project/" + encodeURIComponent(str));
}
}
×

Success!

Help @myuser spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...