How to allow string to contain ' in javascript function
Clash Royale CLAN TAG#URR8PPP
up vote
6
down vote
favorite
I have function that is going to display message containing product name. But problem appeared when Item contained ' inside. Can this be prevented to take whole string as it is and ignore ' character
Calling function
cart.add('<?php echo($imeProizvoda); ?>')
NOT WORKING FOR -> Razer Blade 15'
javascript
var cart =
'add': function(product_id)
addProductNotice('Proizvod dodat u korpu', '<h3>'+product_id+' dodat u <a href="cart.php">korpu</a>!</h3>', 'success');
javascript php html
New contributor
add a comment |Â
up vote
6
down vote
favorite
I have function that is going to display message containing product name. But problem appeared when Item contained ' inside. Can this be prevented to take whole string as it is and ignore ' character
Calling function
cart.add('<?php echo($imeProizvoda); ?>')
NOT WORKING FOR -> Razer Blade 15'
javascript
var cart =
'add': function(product_id)
addProductNotice('Proizvod dodat u korpu', '<h3>'+product_id+' dodat u <a href="cart.php">korpu</a>!</h3>', 'success');
javascript php html
New contributor
Use quotes""
(:
â Ele
2 hours ago
@Ele Not working I tried that already
â minion
2 hours ago
json_encode? painless and flexible
â cske
2 hours ago
add a comment |Â
up vote
6
down vote
favorite
up vote
6
down vote
favorite
I have function that is going to display message containing product name. But problem appeared when Item contained ' inside. Can this be prevented to take whole string as it is and ignore ' character
Calling function
cart.add('<?php echo($imeProizvoda); ?>')
NOT WORKING FOR -> Razer Blade 15'
javascript
var cart =
'add': function(product_id)
addProductNotice('Proizvod dodat u korpu', '<h3>'+product_id+' dodat u <a href="cart.php">korpu</a>!</h3>', 'success');
javascript php html
New contributor
I have function that is going to display message containing product name. But problem appeared when Item contained ' inside. Can this be prevented to take whole string as it is and ignore ' character
Calling function
cart.add('<?php echo($imeProizvoda); ?>')
NOT WORKING FOR -> Razer Blade 15'
javascript
var cart =
'add': function(product_id)
addProductNotice('Proizvod dodat u korpu', '<h3>'+product_id+' dodat u <a href="cart.php">korpu</a>!</h3>', 'success');
javascript php html
javascript php html
New contributor
New contributor
New contributor
asked 2 hours ago
minion
605
605
New contributor
New contributor
Use quotes""
(:
â Ele
2 hours ago
@Ele Not working I tried that already
â minion
2 hours ago
json_encode? painless and flexible
â cske
2 hours ago
add a comment |Â
Use quotes""
(:
â Ele
2 hours ago
@Ele Not working I tried that already
â minion
2 hours ago
json_encode? painless and flexible
â cske
2 hours ago
Use quotes
""
(:
â Ele
2 hours ago
Use quotes
""
(:
â Ele
2 hours ago
@Ele Not working I tried that already
â minion
2 hours ago
@Ele Not working I tried that already
â minion
2 hours ago
json_encode? painless and flexible
â cske
2 hours ago
json_encode? painless and flexible
â cske
2 hours ago
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
7
down vote
accepted
You could use a template literal instead of '
delimiters, allowing you to use both '
and "
(relatively common characters), but you would have to escape backticks (which are relatively uncommon):
cart.add(`<?php echo($imeProizvoda); ?>`)
If you want to be able to use backticks as well, you can replace all backticks in your PHP variable with a backslash plus that backtick:
<?php echo(str_replace('`', '\`', $imeProizvoda)); ?>
I gave an upvote but wondering how to contain all`
,'
and"
in a string?
â Chaska
2 hours ago
Thanks. Is there solution for backticks(just asking)
â minion
2 hours ago
@Chaska You can usestr_replace
to replace backticks with escaped versions
â CertainPerformance
2 hours ago
add a comment |Â
up vote
0
down vote
I have faced this problem before, and solved it using json_encode() on the PHP response, like this:
cart.add(<?php echo json_encode($imeProizvoda);?>)
What doesjson_encode
do exactly in this example and is there more usages of it
â minion
2 hours ago
Returns a strnig with the JSON representation of the argument: json_encode(). The method will scape characters like ' automatically.
â D. Smania
1 hour ago
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
7
down vote
accepted
You could use a template literal instead of '
delimiters, allowing you to use both '
and "
(relatively common characters), but you would have to escape backticks (which are relatively uncommon):
cart.add(`<?php echo($imeProizvoda); ?>`)
If you want to be able to use backticks as well, you can replace all backticks in your PHP variable with a backslash plus that backtick:
<?php echo(str_replace('`', '\`', $imeProizvoda)); ?>
I gave an upvote but wondering how to contain all`
,'
and"
in a string?
â Chaska
2 hours ago
Thanks. Is there solution for backticks(just asking)
â minion
2 hours ago
@Chaska You can usestr_replace
to replace backticks with escaped versions
â CertainPerformance
2 hours ago
add a comment |Â
up vote
7
down vote
accepted
You could use a template literal instead of '
delimiters, allowing you to use both '
and "
(relatively common characters), but you would have to escape backticks (which are relatively uncommon):
cart.add(`<?php echo($imeProizvoda); ?>`)
If you want to be able to use backticks as well, you can replace all backticks in your PHP variable with a backslash plus that backtick:
<?php echo(str_replace('`', '\`', $imeProizvoda)); ?>
I gave an upvote but wondering how to contain all`
,'
and"
in a string?
â Chaska
2 hours ago
Thanks. Is there solution for backticks(just asking)
â minion
2 hours ago
@Chaska You can usestr_replace
to replace backticks with escaped versions
â CertainPerformance
2 hours ago
add a comment |Â
up vote
7
down vote
accepted
up vote
7
down vote
accepted
You could use a template literal instead of '
delimiters, allowing you to use both '
and "
(relatively common characters), but you would have to escape backticks (which are relatively uncommon):
cart.add(`<?php echo($imeProizvoda); ?>`)
If you want to be able to use backticks as well, you can replace all backticks in your PHP variable with a backslash plus that backtick:
<?php echo(str_replace('`', '\`', $imeProizvoda)); ?>
You could use a template literal instead of '
delimiters, allowing you to use both '
and "
(relatively common characters), but you would have to escape backticks (which are relatively uncommon):
cart.add(`<?php echo($imeProizvoda); ?>`)
If you want to be able to use backticks as well, you can replace all backticks in your PHP variable with a backslash plus that backtick:
<?php echo(str_replace('`', '\`', $imeProizvoda)); ?>
edited 2 hours ago
answered 2 hours ago
CertainPerformance
55.1k142848
55.1k142848
I gave an upvote but wondering how to contain all`
,'
and"
in a string?
â Chaska
2 hours ago
Thanks. Is there solution for backticks(just asking)
â minion
2 hours ago
@Chaska You can usestr_replace
to replace backticks with escaped versions
â CertainPerformance
2 hours ago
add a comment |Â
I gave an upvote but wondering how to contain all`
,'
and"
in a string?
â Chaska
2 hours ago
Thanks. Is there solution for backticks(just asking)
â minion
2 hours ago
@Chaska You can usestr_replace
to replace backticks with escaped versions
â CertainPerformance
2 hours ago
I gave an upvote but wondering how to contain all
`
, '
and "
in a string?â Chaska
2 hours ago
I gave an upvote but wondering how to contain all
`
, '
and "
in a string?â Chaska
2 hours ago
Thanks. Is there solution for backticks(just asking)
â minion
2 hours ago
Thanks. Is there solution for backticks(just asking)
â minion
2 hours ago
@Chaska You can use
str_replace
to replace backticks with escaped versionsâ CertainPerformance
2 hours ago
@Chaska You can use
str_replace
to replace backticks with escaped versionsâ CertainPerformance
2 hours ago
add a comment |Â
up vote
0
down vote
I have faced this problem before, and solved it using json_encode() on the PHP response, like this:
cart.add(<?php echo json_encode($imeProizvoda);?>)
What doesjson_encode
do exactly in this example and is there more usages of it
â minion
2 hours ago
Returns a strnig with the JSON representation of the argument: json_encode(). The method will scape characters like ' automatically.
â D. Smania
1 hour ago
add a comment |Â
up vote
0
down vote
I have faced this problem before, and solved it using json_encode() on the PHP response, like this:
cart.add(<?php echo json_encode($imeProizvoda);?>)
What doesjson_encode
do exactly in this example and is there more usages of it
â minion
2 hours ago
Returns a strnig with the JSON representation of the argument: json_encode(). The method will scape characters like ' automatically.
â D. Smania
1 hour ago
add a comment |Â
up vote
0
down vote
up vote
0
down vote
I have faced this problem before, and solved it using json_encode() on the PHP response, like this:
cart.add(<?php echo json_encode($imeProizvoda);?>)
I have faced this problem before, and solved it using json_encode() on the PHP response, like this:
cart.add(<?php echo json_encode($imeProizvoda);?>)
answered 2 hours ago
D. Smania
61814
61814
What doesjson_encode
do exactly in this example and is there more usages of it
â minion
2 hours ago
Returns a strnig with the JSON representation of the argument: json_encode(). The method will scape characters like ' automatically.
â D. Smania
1 hour ago
add a comment |Â
What doesjson_encode
do exactly in this example and is there more usages of it
â minion
2 hours ago
Returns a strnig with the JSON representation of the argument: json_encode(). The method will scape characters like ' automatically.
â D. Smania
1 hour ago
What does
json_encode
do exactly in this example and is there more usages of itâ minion
2 hours ago
What does
json_encode
do exactly in this example and is there more usages of itâ minion
2 hours ago
Returns a strnig with the JSON representation of the argument: json_encode(). The method will scape characters like ' automatically.
â D. Smania
1 hour ago
Returns a strnig with the JSON representation of the argument: json_encode(). The method will scape characters like ' automatically.
â D. Smania
1 hour ago
add a comment |Â
minion is a new contributor. Be nice, and check out our Code of Conduct.
minion is a new contributor. Be nice, and check out our Code of Conduct.
minion is a new contributor. Be nice, and check out our Code of Conduct.
minion is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52732291%2fhow-to-allow-string-to-contain-in-javascript-function%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Use quotes
""
(:
â Ele
2 hours ago
@Ele Not working I tried that already
â minion
2 hours ago
json_encode? painless and flexible
â cske
2 hours ago