Javascript的“document.write()”中的PHP代码 [英] PHP code inside a Javascript's "document.write()"
问题描述
我需要在Javascript的 document.write()
中编写一些PHP代码。这是一个虚拟示例,但是将来Javascript会自动生成PHP代码。
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.
这是Hello World我编码的:
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>
但是,没有显示任何内容,在DOM文件中我得到这个:
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>
任何帮助?谢谢
推荐答案
这是不可能的。 Php需要服务器端运行,并且在客户端上运行javascript。
你可以做的是准备一个php代码来写一个文件,并通过javascript调用这个代码。
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.
这篇关于Javascript的“document.write()”中的PHP代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!