Why can't I see my php code rendered on page? - php

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

Related

PHP files not executing inside include statements

I have an extraordinarily simple website:
index.html contains:
<html>
<body>
<h1>Welcome to my home page!</h1>
<p>Some text.</p>
<p>Some more text.</p>
<?php include 'footer.php';?>
</body>
</html>
It is in /usr/share/nginx/html
footer.php is in the same directory and is:
<?php
echo "<p>printed!!</p>";
?>
When I go to http://MY_IP_address/footer.php I get the proper output of just a blank page with "printed!!" at the top left.
When I go to http://MY_IP_address/ I get:
"
Welcome to my home page!
Some text.
Some more text.
"
But I do not get "printed!!" written anywhere.
I am assuming that somewhere I missed something in my config files, but I can't figure out what it is.
The file which contained include() is a file with .html extension, thus it's not interpreted by Apache. Thus, the code in index.html was not included. (Credits to #RiggsFolly for the improved explanation)
Unless your server is set to parse .html files as PHP, it will not be included.
Thus you'll need to rename it to index.php.
The include() function in PHP includes code from other PHP files and copies them into the file that uses the include statement.
Source:
https://stackoverflow.com/tags/php-include/info
http://php.net/manual/en/function.include.php
Php tags(<?php ?>) can only be parsed when file extension is php.
So,
Change file name from index.html to index.php

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!
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.

Index file conflict with include files

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.

Displaying Php echo message in an HTML page

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...

PHP include Statement Issue

I have a menu.php file I need included in each page on a site I am building. When I open the menu.php by itself it works fine. It's when I have it included in other files such as index.php then nothing loads.
Below is my index.php content. My menu.php is strictly html with a .css style sheet linked. I have searched and can find nothing to solve my problem. Any suggestions?
<html>
<head>
<!-- ... -->
</head>
<body>
<?php include 'menu.php'; ?>
</body>
</html>
EDIT: that is menu.php in the include, not header.php.
Let me try to explain how directories work:
ROOTFOLDER--
home.html
contact.html
PHPFOLDER---
header.php
CSS-----
stylesheet.css
if in home.html you have <?php include 'header.php';?> that wont work because the header is in the PHPFOLDER so you must <?php include 'folderphp/header.php';?>
Check your path, inclusion location etc.
For example, if a file located in a certain folder includes a file, that also includes another file.. the relative path is always based on the very original file opened.
What I do:
include $_SERVER['DOCUMENT_ROOT'].'/menu.php'; // if file is at /public_html/menu.php
or
include $_SERVER['DOCUMENT_ROOT'].'/includes/menu.php'; // if file is at /public_html/includes/menu.php
THat way no matter where you're opening from, you're calling an absolute path.

Categories