echo "<button onClick='follow(".$name.");'></button>";
I need to pass a string as a parameter in follow(user) function onClick event jquery. But it’s getting called as a value.
I tried kind of everything, but in php it looks a bit of a big deal for me. Is there any other way around to get the expected result as a string from a php variable.
2
Answers
Quotes are off and if you’re passing a string you need quotes wrapping the string in the function call.
There is various ways to do it, for standard
"
in html properties:for single quotes
But that’s presuming your users are nice, a crafty user may create a username with
n
in it, then from POSTing to storing and retrieving it would most likely be rendered as a new line:Rendering the following which would cause the page to break:
Or worse a username like:
Which would render a stored XSS:
So a better solution then to directly pass it in, would be to escape it, using
htmlentities
andjson_encode
son
is not rendered by the html.Which would render to:
Though you should be validating usernames on create before allowing such an attack.
You echo a php variable in javascript without adding quotes thus ending with a javascript variable name instead of a string.
Just add escaped quotes like this: