简体   繁体   English

如何在.html文件中包含.php或.html文件?

[英]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. 说我有我的文件index.html ,我想在其中包含a.phpb.html How exactly will I go by doing this? 我将如何做呢?

Here's what I got in index.html : 这是我在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: 这是我的a.php文件:

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

And here's my b.html file: 这是我的b.html文件:

<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 ? 我需要做什么才能使index.html与其a.phpb.html内容一起显示? Currently, the little part with include a.php isn't working, so I thought I'd ask. 目前,包含a.php的一小部分无法正常工作,所以我想问一下。

Thanks 谢谢

First thing: Change the extension of index.html to index.php . 第一件事:将index.html的扩展名更改为index.php Otherwise the server isn't going to know there's PHP to be parsed. 否则,服务器将不会知道要解析的PHP。

Second, you don't need to repeat the html , body , head and other document tags that are already in the index.php file. 其次,您无需重复index.php文件中已存在的htmlbodyhead和其他文档标签。 With the includes, you're inserting data into that file, not creating new documents. 使用includes,您可以将数据插入到该文件中,而不是创建新文档。

<!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: 修改a.php文件:

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

Revised b.php file: 修改后的b.php文件:

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

You have three solutions : 您有三种解决方案:

  1. change the extension of index.html to index.php index.html的扩展名更改为index.php

  2. OR you have to include a.php via iframe . 或者,您必须通过iframe包含a.php

  3. OR you have to call a.php via ajax and then put response where u want. 或者,您必须通过ajax调用a.php ,然后将响应放在您想要的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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