PHP code inside a Javascript's "document.write()"
Solution 1
It's impossible. Php need server-side to runs, and javascript runs just on client side. What you can do is prepare one php code to write one file and call this code by javascript.
Solution 2
The PHP gets evaluated before the JavaScript, so no need to escape the quotes.
document.write("<?php echo "Hello World"; ?>");
Solution 3
when javascript runs that means the response is showing to the browser not compiling the php code any more so as you are putting php code in javascript that means you want something from server than no other alternate but ajax.
Solution 4
What you ask is not possible. Javascript generates the php code client side, after the server is finished. The server never sees the PHP code.
Admin
Updated on August 13, 2020Comments
-
Admin almost 4 years
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