/    Sign up×
Community /Pin to ProfileBookmark

escaping single quotes in function call

I have a page that is populated by a database. The data base includes apostrophes (single quotes) that are escaped by ‘ The php document that populates this page creates a function call like so:

[CODE]onclick=”sendToCart(‘102′,’Lover’s Combo’,’5.00′,’13’,”)”[/CODE]

Obviously you can see the problem with the second parameter. How can I fix this? I need to preserve the ‘

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@KorMar 23.2009 — 
Obviously you can see the problem with the second parameter. How can I fix this? I need to preserve the '[/QUOTE]

I can't see the problem... You need to preserve it? Preserve it. What prevents you from doing that? If you want to echo, in php, a backslash, you must escape it as well:
<i>
</i>echo "onclick="sendToCart('102','Lover[COLOR="Blue"][/COLOR]'s Combo','5.00','13','')"";
Copy linkTweet thisAlerts:
@cfgcjmauthorMar 23.2009 — the problem is that this: 'Lover's Combo'

is the second parameter; there are technically three single quotes...are you saying adding another slash will allow the js to pass it properly?
Copy linkTweet thisAlerts:
@KorMar 23.2009 — are you saying adding another slash will allow the js to pass it properly?[/QUOTE]
yes.
Copy linkTweet thisAlerts:
@KorMar 23.2009 — I don't see the problem.
<i>
</i>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;untitled&lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;
&lt;meta http-equiv="Content-Style-Type" content="text/css"&gt;
&lt;meta http-equiv="Content-Script-Type" content="text/javascript"&gt;
&lt;script type="text/javascript"&gt;
function sendToCart(a,b,c,d,f){
alert(b)
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;a href="#" onclick="sendToCart('102','Lover's Combo','5.00','13','');return false"&gt;send to chart&lt;/a&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@cfgcjmauthorMar 23.2009 — Two does not work.

the parameter in plain text should be Lover's Combo

Because of php/mySql we have to add one slash making it Lover's Combo

Because of this JS is trying to break up the parameters for this

onclick="sendToCart('102','Lover's Combo','5.00','13','')"

as such:

Parameter 1: 201

Parameter 2: Lover

Parameter 3: s Combo

...
Copy linkTweet thisAlerts:
@KorMar 23.2009 — 

Because of this JS is trying to break up the parameters for this

onclick="sendToCart('102','Lover's Combo','5.00','13','')"

as such:

Parameter 1: 201

Parameter 2: Lover

Parameter 3: s Combo

...[/QUOTE]

No, it does not. have a look:
<i>
</i>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;untitled&lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;
&lt;meta http-equiv="Content-Style-Type" content="text/css"&gt;
&lt;meta http-equiv="Content-Script-Type" content="text/javascript"&gt;
&lt;script type="text/javascript"&gt;
function sendToCart(a,b,c,d,f){
for(var i=0; i&lt;arguments.length;i++){
alert(arguments[i])
}
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;a href="#" onclick="sendToCart('102','Lover's Combo','5.00','13','');return false"&gt;send to chart&lt;/a&gt;
&lt;/body&gt;
&lt;/html&gt;
×

Success!

Help @cfgcjm 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 6.17,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...