PHP code inside Javascript "document.write ()"

I need to write PHP code inside Javascript " document.write()". This is a dummy example, but in the future, Javascript will automatically generate this PHP code.

This is the Hello World that I encoded:

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 the following:

<html>
<body>
<script type="text/javascript">
<!--?php echo  "Hello World"; ?-->
</script>
</body>
</html>

Any help? Thanks

+5
source share
5 answers

It's impossible. Php requires server support, and javascript only works on the client side. What you can do is prepare one PHP code to write one file and call this code using javascript.

+3
source

PHP gets evaluated before JavaScript, so no need to hide quotation marks.

document.write("<?php echo "Hello World"; ?>");
+8

javascript, , , php-, , PHP- javascript, , - , , ajax.

+4

, , . Javascript php- . PHP.

+3

PHP- javascript, , PHP -, (.. ), AJAX script , .

+1

All Articles