Skip to content Skip to sidebar Skip to footer

Passing Strings Having Special Characters

I am trying to pass strings with spaces and special characters, but its getting error and nothing is working. e.g.

HTML

<imgonclick='addKeyword("celebritynews&amp;gossip");'src="images/plus.png" />

JavaScript

Use encodeURIComponent(s).

var url = "addKeyword.php?uid=" + encodeURIComponent(user_id) + "&categoryId=" + encodeURIComponent(categoryId) + "&keyword=" + encodeURIComponent($("#keyword_" + categoryId).attr("value")); 

Solution 2:

try

<imgonclick="addKeyword('<?phpecho htmlspecialchars('celebritynews&gossip'); ?>')"src="images/plus.png" >

Solution 3:

Assuming you are writing this text to the page, you can use htmlspecialchars to ensure it is properly escaped in the HTML.

For example:

<?phpecho htmlspecialchars("celebritynews&gossip"); ?>

Does that help?

Post a Comment for "Passing Strings Having Special Characters"