简体   繁体   中英

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:

在此处输入图片说明

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.

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

You are writing a php code inside html file. html file doesn't evaluate php code.change the extension of file to .php instead of .html by doing so you write both html and php code inside that file.

Reason: 1. An html file does not support php scripts inside it and thus anything written will not be executed and will only be treated as an html markup.

Solution: 1. Just save the .html file as .php file and you are done!(very simple).For example if your file name is index.html, just save it as index.php and all the php scripts inside will be executed.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM