Issues with using PHP within HTML [duplicate] - php

This question already has an answer here:
PHP Codes are displaying in HTML
(1 answer)
Closed 9 years ago.
I was trying to test out PHP in HTML on my localhost using this code (in an .html file):
<!DOCTYPE html>
<html>
<head>
<title>PHP Test</title>
<?php echo '<p>Hello World</p>';
?>
</head>
<body>
</body>
</html>
And this is the output:
Hello World
'; ?>
What am I doing wrong?

You have two choices:
Change it to a .php file.
Configure your webserver to send .html files to the PHP processor.

(in an .html file)
HTML files aren't processed for PHP code server-side. So you're just returning the code as-is instead of actually running it. Unless you've explicitly configured your web server to process HTML files as PHP files (which you probably shouldn't do) then you're going to need to make this a .php file.

If the file extension is .html change it to .php.
If it already is a .php file then change <? to <?php it might be that your server does not accept the php shorhand version.

Add these below lines of code into your .htaccess file
AddType application/x-httpd-php .html
AddType application/x-httpd-php .htm
This will add php to your HTML and HTM filetypes

Related

How do I have to configre XAMPP so that it executds <?php ... ?> in html documents? [duplicate]

This question already has answers here:
PHP code is not being executed, but the code shows in the browser source code
(35 answers)
How can I run a PHP script inside a HTML file?
(8 answers)
Closed 2 years ago.
I am trying to have XAMPP's Apache execute <?php ... ? that is embedded in a HTML document:
<html>
<head><title>abc</title></head>
<body>
<?php
echo("bla");
?>
</body>
</html>
However, the <?php ... ?> part is not "executed" and transferred to the browser verbatim.
I suppose there is a configuration file that I have to change so that the piece of php is executed.
As far as i know XAMPP already includes PHP, but unless you end the script name with .php it is unlikely to be processed by the PHP engine.
If you are trying to use php inside .html file then
try adding .htaccess file or changing apache config with the following line:
AddHandler application/x-httpd-php .html
Or try the following,
Stop the apache service, then add one change in c:\xampp\apache\conf\httpd.conf in the section by adding...
AddType application/x-httpd-php .html .htm
Restart apache!
Hope this will help.

php code is not working inside .html file

I have added php code in html file and run this file as .html extension. For this technique i have added below code in .htaccess file.
AddType application/x-httpd-php .html .htm
This whole folder is sub directory of wordpress project folder.When i am run this file in server, pop-up comes and my code file is opens in popup and it will give option to download. where i can see the whole code. What should i do to prevent to pop up box for download and run php code in html file and open as .html extension.
Can anyone help me what should i do?
In order to use php in .html files, you must associate them with your PHP processor in your HTTP server's config file. In Apache, that looks like this:
AddHandler application/x-httpd-php .html
Your php block of codes must be inside the tag
<?php
// Your php code might not work here
?>
<!DOCTYPE html>
<html>
<?php
// Your php code will work here
?>
<head>
<title>Title</title>
</head>
<body>
<?php
// Your php code will also work here
?>
</body>
</html>

Run PHP in HTML page

I am trying to run PHP in HTML page.
I saved this file in WAMP as a .html
<html>
<body>
<?php echo "My first PHP script!";?>
</body>
</html>
But when I open it with the browser and I do inspect element the result is this:
<html>
<head></head>
<body>
<!--?php
echo "My first PHP script!";
?-->
</body>
</html>
If Apache is not running .html pages as php scripts then running a .html page won't run as .php
Solution: Rename your filename from something.html to something.php
or alternatively something.phtml although this is uncommon
You can also save the page as .phtml. This format is possible of displaying .php & .html.
Take a look here for more information: What is phtml, and when should I use a .phtml extension rather than .php?
You should save it as .php instead of .html
You need to put it into a .php file, the webserver (if not configured so) won't interpret .html files as PHP.
Like others said, by changing file extension from .html to .php would work. However, if you don't want to do that, you may want to check this out. It allows you to execute php file without change file extension.

include php script inside HTML

I am a newbie and have never used PHP before. I want to execute PHP script from an HTML file on Linux. What do I need to do?
This is the content of my HTML file:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<HTML>
<HEAD>
<TITLE>Testing Weather Data</TITLE>
</HEAD>
<BODY>
<div id="weather">
<p>Weather Information
<?php include "/home/tahoang/Desktop/weatherData.php"; ?>
</div>
</BODY>
</HTML>
What output do you receive? Does it just show the PHP code?
I assume that's the case.
For a test, change the extension on the file to .php and run it again. Does it work now?
If so, you will need to associate PHP with .html and .htm files on your server.
EDIT
This is the line you want to add to your Apache config:
AddType application/x-httpd-php .html
You can embed the php code right into HTML using <?php ?> tag and putting your php code between opening and closing tags.
Before that you need to add this handler in /etc/apache2/apache2.conf file to run php scripts inside .html file:
AddType application/x-httpd-php .html
You either run pass the file path as an argument to the php command line program, or you configure a web server to execute the page as PHP (the specifics of which depend on the web server you use) then visit the URL of the page.
Here is the step by step process to include php code in html file ( Tested )
If PHP is working there is only one step left to use PHP scripts in files with *.html or *.htm extensions as well. The magic word is ".htaccess". Please see the Wikipedia definition of .htaccess to learn more about it. According to Wikipedia it is "a directory-level configuration file that allows for decentralized management of web server configuration."
You can probably use such a .htaccess configuration file for your purpose. In our case you want the webserver to parse HTML files like PHP files.
First, create a blank text file and name it ".htaccess". You might ask yourself why the file name starts with a dot. On Unix-like systems this means it is a dot-file is a hidden file.
(Note: If your operating system does not allow file names starting with a dot just name the file "xyz.htaccess" temporarily. As soon as you have uploaded it to your webserver in a later step you can rename the file online to ".htaccess")
Next, open the file with a simple text editor like the "Editor" in MS Windows. Paste the following line into the file:
AddType application/x-httpd-php .html .htm
If this does not work, please remove the line above from your file and paste this alternative line into it, for PHP5:
AddType application/x-httpd-php5 .html .htm
Now upload the .htaccess file to the root directory of your webserver. Make sure that the name of the file is ".htaccess". Your webserver should now parse *.htm and *.html files like PHP files.
You can try if it works by creating a HTML-File like the following. Name it "php-in-html-test.htm", paste the following code into it and upload it to the root directory of your webserver:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Use PHP in HTML files</TITLE>
</HEAD>
<BODY>
<h1>
<?php echo "It works!"; ?>
</h1>
</BODY>
</HTML>
Try to open the file in your browser by typing in: http://www.your-domain.com/php-in-html-test.htm (once again, please replace your-domain.com by your own domain...)
If your browser shows the phrase "It works!" everything works fine and you can use PHP in .*html and *.htm files from now on. However, if not, please try to use the alternative line in the .htaccess file as we showed above. If is still does not work please contact your hosting provider.
I am surprised no-one has mentioned a way to hack it - if you don't want to hassle of changing the Apache config here is how you do it:
In your .html file you simply make use out of iframes
<iframe name="myPHPScript" src="myScript.php" width="100%" frameborder="0"></iframe>
This will simply just display what ever is displayed as if you went directly to www.mysite.com/myScript.php
So for example we could have
<?php
echo 'Hello World!';
?>

New to PHP, can I just insert PHP in code?

Let's say I just want to use PHP include to grab HTML from another file. can I just put in that little PHP script and name my .html file (index.html) to index.php and it'll work? I thought I'd have to add my server password an other info in PHP to use it.. What do I do?
No you can't just insert PHP into HTML and expect it to work.
PHP is a server side language that generates the HTML that's sent to the client's web browser. The files usually have the extension ".php" rather than ".html" but simply renaming "html" as "php" won't work.
You need to have a PHP parser installed on your server and reconfigure your whole site.
You might be thinking of JavaScript which can be inserted into HTML and is run client side.
Exactly as you said:
<html>
<head>
<title>Test PHP file</title>
</head>
<body>
<?php
echo 'test';
//all your php code can go in here
?>
</body>
</html>
You can have multiple <?php ?> blocks in your file.
This will all work if your server is configured to parse HTML with the PHP interpreter.
I made an Apache handler including
application/x-httpd-php5 .html .htm
Then yes. You would be correct. I suggest doing this with an Apache .htaccess or a hosting panel.
If your server is configured to parse php files, then yes, adding <?php include('somefile.html'); ?> should work fine.
no you don't need server password to run php... just rename the file to .php and insert your php code inside <?php ?>
PHP and HTML
test.php
<?php
define('title','foo');
?>
<!doctype HTML>
<html>
<head>
<title><?=foo?></title>
</head>
<body>
...
</body>
</html>

Categories