在另一个 PHP 文件的 JavaScript 中显示 PHP 文件内容


display php file content in the javascript of another php file

所以基本上我想html()一个php文件,但我似乎失败了,也许这是不可能的,但我想确保。

这是一个示例代码(不是真实的代码):
page.php

<? //some code generating html ?>
<div class='content'></div>
<script type='text/javascript'>
    $('.content').html("<? include('php/content.php'); ?>");
</script>    

php/content.php

<div class='realContent'>Awesome Stuff Here</div>

注意:我知道有.load()$.post和许多其他。我只想知道这种做法是否可行,.html() php 文件的内容。
提前感谢您,很棒的社区!

d3nm4k.我示例中的代码执行您想要的操作。也许您遇到了<的短开头标签的问题?>.我尝试在我的示例中加载的文件位于 app/View/Tests/def.php 中,并且工作正常。

希望对您有所帮助!

<div class='content'></div>
<?php 
$fileLocation = APP . 'View/Tests/def.php';
// Ensure the file exists
echo "FILE EXISTS: <b>" . (file_exists($fileLocation) ? "TRUE" : "FALSE") . "</b><br /><br />";
?>
<?php 
// Alternatively, you can try this one
//include $fileLocation; 
?>
<script type='text/javascript'>
$('.content').html("<?php include($fileLocation); ?>");
</script>    

你可以做到,你给的代码我只是通过添加?php来尝试

<div class='content'></div>
<script type='text/javascript'>
    $('.content').html("<?php include('php/content.php'); ?>");
</script>    

当 php 在服务器上执行时,它会在 html() 函数中用实际的 html 替换内容。在客户端,JavaScript扮演着渲染它的角色。