传递具有特殊字符的字符串

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

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

but the '&' inside string celebritynews&gossip is breaking the code.

I figured out that the problem is in addKeyword function, I'd used the following code for defining a variable named url,

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

and that $("#keyword_" + categoryId) is causing problem. Any way to solve that?

Assuming you are passing the string on a URL, the parameter values need to be URL encoded.

...SUMMARY EDIT...

The & is interpreted as HTML and must be HTML encoded, specifically &amp;

HTML

<img onclick='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")); 

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

For example:

<?php echo htmlspecialchars("celebritynews&gossip"); ?>

Does that help?

try

<img onclick="addKeyword('<?php echo htmlspecialchars('celebritynews&gossip'); ?>')" src="images/plus.png" >