Christian Dela Pena Christian Dela Pena - 6 months ago 22
PHP Question

Print JavaScript code in PHP

I can't echo the code below because of "" and '' how can I fix this?
Is there another way to print javascipt code on php without using "" and ''?

print "<a href ="javascript:void(0)" onclick = "document.getElementById('light').style.display='block';document.getElementById('fade').style.display='block'">Add to Cart</a>";

Answer

Escape " with a \

print "<a href=\"javascript:void(0)\" onclick=\"document.getElementById('light').style.display='block';document.getElementById('fade').style.display='block'\">Add to Cart</a>";

You can use HEREDOC

print <<< EOT
    <a href="javascript:void(0)" onclick="document.getElementById('light').style.display='block';document.getElementById('fade').style.display='block'">Add to Cart</a>
EOT;

Another way is to end PHP before the printed line and continue it after:

?>

<a href="javascript:void(0)" onclick="document.getElementById('light').style.display='block';document.getElementById('fade').style.display='block'">Add to Cart</a>

<?php