Chronaium Chronaium - 5 months ago 15
PHP Question

Javascript string in php not working (SOLVED)

I'm new to PHP and I don't understand why this piece of code doesn't work. Can somebody please help me?

$alertver = '<div class="col s12 '. $_POST["alert"] .'"><span class="closebtn" onclick="this.parentElement.style.display='none';">&times;</span>'. $textstrong . $_POST["alerttext"] ."</div>";


When I try to select this from my mysql database the 'none' part of this code doesn't work.

When I try to use this code:
$alertver = '<div class="col s12 '. $_POST["alert"] .'">
<span class="closebtn" onclick="this.parentElement.style.display=\'none\';">&times;</span>'. $textstrong . $_POST["alerttext"] ."</div>";


It gives me this MySQL error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'none';">&times;</span><strong>1233</strong> 56</div>'


Thanks in advance!

Answer

In your onclick you have single quote you should escape with proper escape char \

$alertver = '<div class="col s12 ' . $_POST["alert"] .'">
                <span class="closebtn" 
                onclick="this.parentElement.style.display=\'none\'; ">&times;</span>'
                . $textstrong . $_POST["alerttext"] ."</div>";

If you store the data in bb you can try

$alertver = '<div class="col s12 ' . $_POST["alert"] .'">
                <span class="closebtn" 
                onclick="this.parentElement.style.display=  '  .
                          'none'  .  '; ">&times;</span>'
                . $textstrong . $_POST["alerttext"] ."</div>";