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!
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>
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
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.
Just change index.html to index.php.
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.
Related
I decided to build a booking calendar and embed it to my website. But I don't know how to include it in the html file. I tried the <?php ?> tag but it doesn't display at all. I looked around and found this htaccess file thing. How do I generate it? Is it a way to use my php code without this?
Here is what included in my big html file (I did not include other html here)
<!DOCTYPE html>
<hmtl>
<head>
</head>
<body>
<?php
include 'calendar.php';
$calendar = new Calendar();
echo $calendar->show();
?>
</body>
</html>
I used sublime text edit and there is no "color" on the key words meaning it is not recognizing the php tag.
Place the php code of the calendar in a file and save it as say, calendar.php and then add the following line in the pages where you want the calendar to appear in:
<?php include("calendar.php"); ?>
N.B. Make sure to replace the file extension of all your html files with .php extension instead of the .html extension.
For example: index.html should be index.php
I see multiple answers about creating php files in order to reuse headers/footers, but nothing specific enough. I can't seem to get it to work.
What exactly would the php file look like and what exactly would my html file look like (given the code as it currently is, below)? do I have to convert all my html files to php files in order to use the php include line?
<div id="footer2-wrap">
<div class="container">
<table id="header">
<tr>
<td class="phone-number"><span class='wsite-text wsite-phone'>Copyright 2015 | xxx Corporation</span></td>
</tr>
</table>
</div>
Create a new file with your footer data. Let's give it the name: footer.php:
<div id="footer2-wrap">
<div class="container">copyright etc...</div>
<div>
Then inside your master template (or index.php file), include the footer file:
include_once('footer.php');
You should work with include(), require() or require_once functions in PHP to include your files, which depends on your situation.
For instance, let's assume you have a basic php file, called index.php and you want to add sidebar.php, footer.php, navigation.php.
index.php
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8">
<link rel="stylesheet" href="style.css"/>
</head>
<body>
<?php
include("sidebar.php");
include("navigation.php");
include("footer.php"); // we include footer.php here. you can use .html extension, too.
?>
</body>
</html>
footer.php (or html)
About us
Our work
Testimonials
What we do
Contact us
Yes, you must have a .php file to use PHP, your server must also have PHP installed (but most already do). PHP also adds to the loading time of a page, so take that into consideration when using it. To add the footer with PHP, you can use the PHP function include(), or, I am not sure if this is considered correct, with file-get-contents():
include():
<?php
include("footer.html");
?>
file-get-contents():
<?php
$footer = file_get_contents('footer.html');
echo $footer;
?>
You could also do the same thing with JavaScript:
var xmlhttp, text;
xmlhttp = new XMLHttpRequest();
xmlhttp.open('GET', 'http://www.example.com/file.txt', false);
xmlhttp.send();
text = xmlhttp.responseText;
document.getElementById("footer").innerHTML=text;
Code taken from here.
Note: The file must be on the same domain to use JS.
First create a file called menu.php(if you use .html it wont work)
On that php file write the html code for your menu
<div id="navbar">
more code
</div>
At your main html file write
<?php
include("menu.php");
?>
The code should be able to run. Make sure you are running the code via Apache. Download Xammp or wammp. Start Apache, copy paste your project folder to the xammp folder under httdocs. Then on your browser type in localhost/[your project name] and make sure the html files are changed to .php.
I have a problem with my index.php together with my "include files". My index.php file is located inside the main folder named mysite and the rest inside it are subfolders with the respective .php files. The sub-folder files are working perfectly, except the index.php. The include files are messed up every time I preview it in the browser.
The path for the index.php file goes like this:
mysite/index.php
The path for the include files goes like this:
mysite/pages/include/header.php
Here is the HTML file with the include files:
<html>
<head>
<?php
include ("pages/include/headertop.php");
include ("pages/include/header.php");
include ("pages/include/nav.php");
?>
</head>
</html>
Kindly correct me if I have missed something here.
By the way, I'm using XAMPP.
Thank you and More power!
edit your index.php:
include ("../pages/include/headertop.php");
include ("../pages/include/header.php");
include ("../pages/include/nav.php");
and try again "mysite/index.php"
Firstly make sure that you are calling these include commands in a .php file and not .html file.
Secondly make sure that your server is up and running well.
After this try this code:
<html>
<head>
<?php
include "pages/include/headertop.php";
include "pages/include/header.php";
include "pages/include/nav.php";
?>
</head>
</html>
P.S: just remove the brackets from include.
Please also make sure that the file names are correct and they follow the proper case.
Just getting started with php. In index.php I have
<?php
echo 'test';
?>
and when I go to localhost:8080/php/index.php I see 'test'. However I made an HTML file index.html with this code:
<html>
<body>
<?php include 'index.php'; ?>
<h1>Welcome to my home page!</h1>
</body>
</html>
but all I see is 'Welcome to my home page' and no 'test'. Wondering what I'm missing here.
Thanks!
Change the file extension from .html to .php
Change index.html to index.php
NOTE: You can't execute php code in .html files.
Also one more thing, you need to change both file names. because you are changing index.html to index.php and another file with same name is already there.
so do this.
index_inc.php
<?php
echo 'test';
?>
index.php
<html>
<body>
<?php include 'index_inc.php'; ?>
<h1>Welcome to my home page!</h1>
</body>
</html>
Your file's extension is .html , so php code will not be excuted.
if you really want to let the .html file to excute php code, you can write a .htaccess file
and add the line
AddType application/x-httpd-php .php .html
Hi I am a newbie to web development. I am trying to display echo message from a php file in html page.
PHP File:
<?php
echo "Hello";
?>
Other file:
<form method="get" action="latest.php">
</form>
Note: These both are two different files
I am completely new to this. I don't know where I am going wrong.
Normally, you can only execute PHP code in a file which has .php extension because your webserver is setup like that for PHP. However you can simply tell your web server to parse your HTML files as PHP too and then you can run your PHP code in an HTML file wherever you want. Now assuming your Web server is Apache.
Step 1:
Create a file named .htaccess and place the following code in it and then place this file in your root directory for that website
AddType application/x-httpd-php .html
Step 2:
Go to Your HTML file and add an include statement where you want your PHP file to be included
<form method="get" action="latest.php">
<?php include ("yourPHPfile.php"); ?>
</form>
Step 3:
All done, now go check output in browser.
Just put the code together:
<form method="get" action="latest.php">
<?php echo "Hello"; ?>
</form>
Everything inside the Tags will be interpreted as PHP, no matter where it is. But make sure the ending of the file is .php!
May be try this..
<form method="get" action="latest.php">
<?php include('php_file.php'); ?>
</form>
Please note that other file should also be a .php file. if it is an html file you either need to change it to .php or you need to make ajax request.
You can't execute php codes in a file ending other than .php.
But you can have HTML code displaying in a .php file.
So I would recommend you change the extension to .php.
You can simply have
<?php
$name = "Harry Potter";
?>
<h1>Hello, <?php echo $name;?>!</h1>
Also you need a web server software and php processor. WAMP or XAMP may help you on that.
I am editing my answer since some are really picky.
In general you can't execute php in files which dont have the .php extension. But with some tweaks and line changes you can. I am just stating that you can't since, you mentioned that you are new to web development and I would like to make things simpler rather than confusing
If you want to execute php code in .html file extension than u need to do some htaccess changes.since file extension plays a major role ,it only tells the browser how to handle or parse a particular page.
You can create a .htaccess file at the root folder of your website and in the htaccess file add this line:
AddType application/x-httpd-php .html .htm
If you only plan on including the PHP on one page, it is better to setup this way:
<Files yourwebpage.html>
AddType application/x-httpd-php .html
</Files>
This code will only make the PHP executable on the yourpage.html file, and not on all of your html pages.
If you are ok with changing your file extension to .php than your work will be quite easy.you can simply include a php code between html code.like this
<form method="get" action="latest.php">
<labe><?php echo "hello" ?></label>
</form>
Its so simple you can just put php tag and write php code even inside the html
<form method="get" action="latest.php">
<div><?php echo "Hello";?></div>
</form>
Whenever you echo something, it is printed on html page only.
If you want to echo inside a particular html element, just put the code there. For example,
<html>
<body>
<div class="myDiv1"><?php echo "This is myDiv1"; ?><br></div>
<div class="myDiv2"><?php echo "This is myDiv2"; ?></div>
</body>
</html>
Hope this helps...