I just found myself in an odd position trying to put a string inside of javascript, that is defined in a php. The code I’m going to post isn’t that actual code, but its the situation I’m in.
$script = "
var someVar = \"<input type='text' onchange='callScript('problem here') />' \";
";
so as you see, I’m setting a javascript variable that has html elements in it. the html element has an onchange event that is set to call a function that takes a string as a parameter. How would i insert that parameter?
I think if you call
addslashes()on the string before inserting it into the variable, you should be fine as to escaping any possible unsafe characters, so: