Java Nerd
Java Nerd

Reputation: 968

Include a PHP file inside an HTML file

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!

Upvotes: 0

Views: 3355

Answers (5)

Java Nerd
Java Nerd

Reputation: 968

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.

Upvotes: 0

wbjari
wbjari

Reputation: 161

Just change index.html to index.php.

Upvotes: 0

fast
fast

Reputation: 885

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>

Upvotes: 3

VK321
VK321

Reputation: 5953

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

Upvotes: 1

tomazahlin
tomazahlin

Reputation: 2167

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.

Upvotes: 0

Related Questions