Search code examples
phphtmlmysqlwampmeekro

php not functioning inside html file but works outside


I have been following the php tutorial here

CODE

Here is my html file:

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

This yields the following:

enter image description here

However if I copy the php code into a new postForm.php file and click "Submit" (as you can see the action is postForm.php), it works.

I get my blank screen with 3 words (from the database).

The problem is it is a brand new blank page and I do not want that.

PROBLEM

Why is it that the code works outside the html file, but not inside the html file. Why do I get ".row['post']."";} ?> when inside the html file but I get perfect output when the php exists in its own php file?

There is clearly nothing wrong with the code, so what could it be?

It really confuses me. Thanks for any answers.


Solution

  • Change your file extension .html into .php or .phtml. It will solve your problem.