I need to write some PHP code inside a Javascript's "document.write()
". This is a dummy example, but in the future Javascript will generate automatically that PHP code.
This is the Hello World I have coded:
MyFile.php
<html>
<body>
<script type="text/javascript">
document.write("<?php echo \"Hello World\"; ?>");
</script>
</body>
</html>
However, nothing is displayed, and in the DOM file I get this:
<html>
<body>
<script type="text/javascript">
<!--?php echo "Hello World"; ?-->
</script>
</body>
</html>
Any help? Thanks
The PHP gets evaluated before the JavaScript, so no need to escape the quotes.
document.write("<?php echo "Hello World"; ?>");