I am trying to pass strings with spaces and special characters, but its getting error and nothing is working. eg
<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 &
HTML
<img onclick='addKeyword("celebritynews&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"));
尝试
<img onclick="addKeyword('<?php echo htmlspecialchars('celebritynews&gossip'); ?>')" src="images/plus.png" >
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?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.