gpt4 book ai didi

php 不能在 html 文件中运行,但可以在外部运行

转载 作者:行者123 更新时间:2023-11-29 06:26:07 25 4
gpt4 key购买 nike

我一直在关注 php 教程 here

代码

这是我的 html 文件:

    <!DOCTYPE html>
<html>
<head>

<link rel=”stylesheet” type=”text/css” href=”style.css”>

<form action="postForm.php" method="post">

<TextArea name="microBlog" id="microBlog" cols="30" rows=“10"></TextArea>

<input type="submit">

</form>

</head>

<body>

<?php

require_once 'meekrodb.2.3.class.php';
DB::$user = 'root';
DB::$password = '';
DB::$dbName = 'MicroBlog';
$results = DB::query("SELECT post FROM MicroBlog");
foreach ($results as $row){

echo "<div class='microBlog'>" . $row['post'] . "</div>";
}

?>


</body>

</html>

这会产生以下结果:

enter image description here

但是,如果我将 php 代码复制到一个新的 postForm.php 文件中并单击“提交”(如您所见,该操作是 postForm.php),它会起作用。

我的空白屏幕显示 3 个字(来自数据库)。

问题是它是一个全新的空白页,我不希望这样。

问题

为什么代码在html文件外有效,而在html文件内却不行。为什么我在 html 文件中得到 ".row['post']."";} ?> 但当 php 存在于它自己的 php 文件中时我得到完美的输出?

代码显然没有问题,那会是什么问题呢?

这让我很困惑。感谢您的任何回答。

最佳答案

将文件扩展名 .html 更改为 .php.phtml。它会解决您的问题。

关于php 不能在 html 文件中运行,但可以在外部运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30889486/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com