john
john

Reputation: 139

How do I include a .php or .html file in a .html file?

Say I have my file index.html, and I want to include a.php and b.html in them. How exactly will I go by doing this?

Here's what I got in index.html:

<!DOCTYPE html>
<html>
    <head>
        <title>SQL DB Demo</title>
        <link rel="stylesheet" type="text/css" href="styles.css"/>
    </head>

    <body>

<!--Changes -->

        <?php
            include 'a.php';
        ?>

    </body>


</html>

Here's my a.php file:

<html>
    <head>
        <p>
            Hello there, you rock.
        </p>
    </head>
</html>

And here's my b.html file:

<html>
    <head>
        <p>
            Hello there, you rock even more.
        </p>
    </head>
</html>

What do I need to do to get index.html to display its contents along with the contents of a.php and b.html? Currently, the little part with include a.php isn't working, so I thought I'd ask.

Thanks

Upvotes: 2

Views: 86

Answers (2)

BeingCoder&#39;s
BeingCoder&#39;s

Reputation: 1

You have three solutions :

  1. change the extension of index.html to index.php

  2. OR you have to include a.php via iframe.

  3. OR you have to call a.php via ajax and then put response where u want.

Upvotes: 0

Michael Benjamin
Michael Benjamin

Reputation: 370993

First thing: Change the extension of index.html to index.php. Otherwise the server isn't going to know there's PHP to be parsed.

Second, you don't need to repeat the html, body, head and other document tags that are already in the index.php file. With the includes, you're inserting data into that file, not creating new documents.

<!DOCTYPE html>
<html>
    <head>
        <title>SQL DB Demo</title>
        <link rel="stylesheet" type="text/css" href="styles.css"/>
    </head>

    <body>

<!-- a.php file below -->

        <p>
            Hello there, you rock.
        </p>

<!-- b.php file below -->

        <p>
            Hello there, you rock even more.
        </p>

    </body>


</html>

Revised a.php file:

        <p>
            Hello there, you rock.
        </p>

Revised b.php file:

        <p>
            Hello there, you rock even more.
        </p>

Upvotes: 4

Related Questions