0

Hye There I am new to web and want to include a .php file inside my .html file. This is my .html file:

<html>
<head>

<title>Includer Example</title>
</head>

<body>
    <div style="height:100px; width:300px; border:#F00 thick; background-color:#F00">
        THIS DIV IS IN HTML FILE
    </div>
        <?php
            include 'seconddiv.php';
            ?>
</body>
</html>

and this is my php file:

<html>
<head>

<title>second div</title>
</head>

<body>
    <div style="height:100px; width:300px; background-color:#FF0">
        THIS DIV IS IN PHP FILE
    </div>
</body>
</html>

I am using WampServer and totally new to Web Coding can somebody give any idea what I am doing so wrong please thanks in advance!

4
  • 1
    You cant use php in a .html file.., so you wont be able to include a php file eather inside a .html file. You should make it into a .php file first. Commented May 28, 2014 at 13:36
  • 1
    You can run php code inside php files, or you can configure apache to run html as php, but normally, you can not include php in HTML files Commented May 28, 2014 at 13:37
  • You need to create a PHP file first and then you can load (include) PHP or HTML files into the main PHP file. Commented May 28, 2014 at 13:40
  • 1
    Be aware that even if you follow the solutions below, the result will not be valid HTML, as your tags in the included file will be out of place. Commented May 28, 2014 at 13:41

5 Answers 5

3

Rename your .html to .php, so the PHP processor processes it.

Following Patricks comment above: Note that the (included) .php file by no means need to be a html document. It must contain only exactly what you want to be inserted into the 'main' html document.

The main file needs to be .php in order to run the PHP-processor on it.

index.php:

<html>
 <body>
  <div style="height:100px; width:300px; border:#F00 thick; background-color:#F00">THIS DIV IS IN HTML FILE</div>

  <?php
   include 'seconddiv.html';
  ?>
 </body>
</html>

The included file can have any ending; as it is included into the .php file, it is processed by the PHP processor anyway.

seconddiv.html:

<div style="height:100px; width:300px; background-color:#FF0">
    THIS DIV IS IN PHP FILE
</div>
Sign up to request clarification or add additional context in comments.

4 Comments

although I have renamed my file to seconddiv.php and placed it into wamp/www and changed my include 'localhost:82/seconddiv.php' but its still not working!
@user3411946 let's say that the html file you want to include the php file into is named index. and that the php file you want to include is named index2. (so we have index.html and ndex2.php). You need to rename index.html to index.php and in that include index2.php. And now it should run (if both files are in the same folder you don't need to provide a path, just use include 'index2.php'. (also try with require_once('index2.php'))
its still not working my file name is index.php and the file i want to include is index2.php as you said and i used include 'index2.php' so index2 wont run and showing any div on browser
@user3411946 Can you access index2.php directly? Maybe it's just not working and that's why it's not being included. (did you also try with require?) Can you also please update your question with the content of (or similar to) index2.php? In your example you have an entire html structure/page in there. If that's the case, your browser might not be displaying it as your page would include 2 different HTML tags, one inside the other.
1

convert file from HTML to .php.

Php code will only run in file with php.

PHP + HTML CODE === CAN RUN IN === .php file

PHP + HTML CODE === CANT RUN IN === .html file

2 Comments

This is not true, however you can enable to run PHP code inside .html. Article here: php.about.com/od/advancedphp/p/html_php.htm
No in html you cant run, you can add html code as normal in php file.
0

By default PHP code is not executed in .html file, so apache does not send it to the PHP to execute it. If you rename the .html file to .php, it would be a simple solution.

Comments

0

Just change index.html to index.php.

Comments

0

Edit the .htaccess file How? Well, here’s what you should do:

Go to your Document root or WWW root directory or folder; it commonly looks like this: /home/akiko/public_html

Look for the file named .htaccess. If it’s not there, create a blank page using a regular text editor like Notepad and save the file as .htaccess – the file name includes that little dot in the front.

Now edit this file by adding the following lines:

RemoveHandler .html .htm
AddType application/x-httpd-php .php .html .htm

Save and close the .htaccess file. Upload it to your web server (to your Document/WWW root) and that’s it!

Sample PHP code in a .HTML webpage Now create a test file and name it test.html

Copy the following HTML (containing PHP code) into it:

<html>
    <head>

    </head>
    <body>
        <h1>
            <?php echo "I LOVE PHP!"; ?>
        </h1>
    </body>
</html>

Upload it to your web server and view it with your favourite browser. You will see that it works just fine.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.