2

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

Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
john
  • 139
  • 1
  • 8

2 Answers2

4

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>
Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
  • Are there any drawbacks to me making it a .php file? – john Jan 14 '16 at 01:34
  • For all practical purposes, no. [Read more.](https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=html+vs+php+file) – Michael Benjamin Jan 14 '16 at 01:38
  • 1
    if supported and using Apache, they can instruct it to treat `.html` files as PHP. Plus, they can also (and if supported), use SSI with `.shtml` and include PHP files. I've done it often. – Funk Forty Niner Jan 14 '16 at 01:41
  • @Fred-ii-, agreed. But I'm trying to keep in simple, as it doesn't appear the OP must use `.html` or SSI. Switching to `.php` is simple, efficient and a good habit to get into, in my view. – Michael Benjamin Jan 14 '16 at 01:47
  • OP here, right now I'm using brackets io, and when I switched index.html to index.php, stuff got weird. I couldn't do any live preview any more because of that unless I switched it back to index.html. Any tips? – john Jan 14 '16 at 01:52
  • 1
    @Michael_B FYI: OP reposted http://stackoverflow.com/q/34781037/ it's about brackets.io http://brackets.io/ and not [adobe-brackets](http://stackoverflow.com/questions/tagged/adobe-brackets) and I closed that question. – Funk Forty Niner Jan 14 '16 at 03:21
0

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.