Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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!

share|improve this question
2  
Check the PHP function addslashes() to escape the quotes : php.net/manual/en/function.addslashes.php –  koopajah Dec 4 '12 at 17:12
    
What's in showContent()? –  smclark89 Dec 4 '12 at 17:12
    
Possible duplicate of this - stackoverflow.com/questions/8282708/… –  serupticious Dec 4 '12 at 17:14
    
Thanks, but I want to emphasize that $content can contain HTML code and are you sure with only addslashes() can solve my problem? I tried it but my echo can't even render the label properly, instead, it showed me a part of the whole code. –  King King Dec 4 '12 at 17:22
add comment

1 Answer

Try this:

echo '<label onclick="showContent('.$content.')">Click me</label>'
share|improve this answer
    
Sorry, it will work in some case, the $content's value varies as much as you can imagine, you can think of this, I have a textarea in a form, I want I can paste any HTML code (copy from another editor, for example) to that textarea and submit the form to a php page, this page contains code to render a button (or a label with onclick event as I asked) on which the user can click to show what s/he sent. This is a very simple page, I've done this many times since the time I learned PHP but for normal text, this case is for HTML code (with multiline possibly). –  King King Dec 4 '12 at 17:48
    
@king-king Could you maybe add the showContent function to the question. It is rather hard to help you working semi-blind. Thanks. –  StrayObject Dec 5 '12 at 11:36
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.