I have the following code:
echo '
<td>
<input type="button" name="delete" value="X" onclick="clearSelection(this.form, '.$type.');this.form.submit();" />
</td>'
;
The problem is that I cannot pass a string to the clearSelection() Javascript function, because $type needs to be in parentheses.
I tried it with backslash, u0222, multiple quotes and so on but nothing brought me to the solution.
Solution:
$type = json_escape_string($type);
$raw_text = "clearSelection(this.form, $type); this.form.submit();";
$escaped_text = htmlspecialchars($raw_text);
echo '<td><input type="button" name="delete" value="X" onclick="'.$escaped_text.'" /></td>';
function json_escape_string($str){
$str = strtr($str, array('\\'=>'\\\\',"'"=>"\\'",'"'=>'\\"',"\r"=>'\\r',"\n"=>'\\n','</'=>'<\/'));
return "'".$str."'";
}