【问题标题】:php not functioning inside html file but works outsidephp 在 html 文件中不起作用,但在外部起作用
【发布时间】:2015-06-17 10:53:52
【问题描述】:

我一直在关注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>

这会产生以下结果:

但是,如果我将 php 代码复制到一个新的 postForm.php 文件中并单击“提交”(您可以看到操作是 postForm.php),它就可以工作。

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

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

问题

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

代码显然没有错,那会是什么?

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

【问题讨论】:

  • 你的文件扩展名是什么?
  • 这是一个 .html 文件。当我设置一个操作以使用相同的 php 代码打开一个单独的 .php 文件时,它突然起作用了。但是代码是一样的!

标签: php html mysql wamp meekro


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    您正在 html 文件中编写 php 代码。 html 文件不评估 php 代码。将文件的扩展名更改为 .php 而不是 .html 通过这样做您在该文件中编写 html 和 php 代码。

    【讨论】:

      【解决方案3】:

      原因: 1. html文件不支持其中的php脚本,因此编写的任何内容都不会被执行,只会被视为html标记。

      解决方案: 1. 将.html文件保存为.php文件即可!(很简单)。比如你的文件名是index.html,保存为index.php,里面的php脚本都会被执行。

      index.php:

      <!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>
      

      【讨论】:

        猜你喜欢
        • 2018-07-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多