问题描述:

It's simple if the code is written in Javascript (between the <script> and </script>, but I'm encountering a problem like this (render dynamic content using echo in PHP):

echo "<label onclick='showContent(\"".$content."\")'>Click me</label>"

and I also tried this:

echo '<label onclick="showContent("'.$content.'")">Click me</label>'

The problem is that $content can contain many characters like " (double quote), ' (single quote)... Generally, it contains HTML code.

If it is a normal sentence, the code above will work well, it renders a label of 'Click me' and I can click it to run the function showContent(), but if it contains HTML code, it can render the label however I can't run the function showContent() when I click on it. I think there is some problem with quotes here. I tried replacing all the single and double quotes in $content with " or \" and ' or \' but it still doesn't work, even in some cases, the label of 'Click me' can't be rendered properly, instead of that it shows me almost the code behind.

I'm really stuck at this (already for hours), please give me a solution. Your help would be highly appreciated!

Thanks!

网友答案:

Try this:

echo '<label onclick="showContent('.$content.')">Click me</label>'
相关阅读:
Top