php script works fine, but not inside HTML - php

I've installed WIndows 10 version of XAMPP with MySQL (MariaSQL) & PHP version 5.6.28
The SQL is working just fine, i.e., I can log in, create DATABASE, etc.
php -v works fine on the command line ... PHP 5.6.28 (cli)...
phpinfo.php works as expected.
It tells me display_errors is ON
php.ini = display_errors=On
As a php script: mysql_test.php output to the screen is fine.
<?php
echo "Hello World of PHP!";
echo mysql_connect ('localhost', 'joe', 'gonzo9876');
?>
When I embed it in plain vanilla HTML, i.e., http://localhost/mysql_test.html
The php code won't echo/print on the screen
- and -
when I right-click for viewing the source code, the php code is visible
- and -
the Google debugger has converted the php tags to

Your Apache, by default, will only run files with .php extension as PHP. .html will be displayed to browser as is.
You need to either:
Rename your file from mysql_test.html to mysql_test.php; or
Config your Apache to also treat .html files as PHP script
The later one is an unusual practice. I wouldn't recommend it.
Basically no hosting provider will do it. So even if you make it work in your XAMPP setup, it won't work in any normal shared hosting. So if you potentially need to move your code to a shared hosting, please don't do it.

You can't process the PHP code inside html page with .html extension(without parse). It's only for rendering html, if you wanna use embed/mix both php with html, then use .php extension instead as PHP is server-side scripting language. When talking about server-side language, you need a server either local(xampp,wampp,etc..)/production server to host and run your apps.
Reflect to Commenter's comment :
Another workarounds is by telling the Apache to treat .html as .php and with this, you can mix php code with html by using .html, but it's just kinda a HACK for me(personal perspective). Well the choice is yours.

You need to make extension .php if you want to put php code inside html tags. But if you do not want to show .php, please use .htaccess for url rewrite. You can make file as .php but with .htaccess you can show as .html so user will see it as .html.
RewriteEngine On
RewriteRule ^test.php test.html [R=301,L]
or something like this, please search for url rewrite for more detail.

Related

PHP in html not parsing

I have a html file and I want to run PHP code to call out an array value within HTML form box but the interpreter doesn't recognize
<input value="<?php echo $_SESSION['user']; ?>"/>
p.s. Not sure if it makes any difference but I am running this on cloud9 with apache (httpd).
Save your HTML file as a PHP file since HTML files cannot execute php code. For example if your file is named index.html you want to re-save it as index.php once you do that your php code should run.
HTML files does not parse PHP. You need to have a file with .php as extension to run PHP.
PS: Its possible to make HTML file run PHP as well but that requires some extra settings in apache config which is never enabled by default. Reason being a security threat. But If you have a dedicated server which allows playing with apache config then you can achieve this..
have a look at this LINK
Ok I figured out that it was actually working all along =/ (i was only looking at the local IDE display within cloud9 but the code was working all along since it actually required back-end processing and the local IDE won't display that).

Want to learn how to use php to save data in forms. How come this doesn't work in a html file?

I'm a newbie please bear with me. I just finished learning how to create forms in html. Now, I want to learn how to save and display the data using php. While following along the php exercises in w3, I tried running this file but it didn't work when saved as a .html file. Could someone explain what I'm doing wrong and why the code text won't display? Thanks.
<!DOCTYPE html>
<html>
<body>
<h1>My first PHP page</h1>
<?php
echo "Hello World!";
?>
</body>
</html>
Your server is not setup to parse PHP in files with a .html extension. Add an .htaccess file to your web root with the following lines in it -
# allows HTML files to be interpretted as PHP files
AddType application/x-httpd-php .html
You should save it with a .php extension. I assume you're working locally and you would need to install PHP on your machine. The best way to install it locally is by using XAMPP or WAMP to emulate a server. I prefer using XAMPP https://www.apachefriends.org/index.html. Install it start the apache service, put this file in your htdocs folder i.e. 'root' and navigate to 127.0.0.1 from the browser. This is a start, even if you put it on an "actual" server it'll work fine.
First off, you need to change your file extension from .html to .php
Are you running a web server? If not, you need to either be running a server like MAMP
just done you a quick form here
Copy and paste it to where ever you run you script,make sure it has a php extension, and see if it gives you the same result as it does in the link. If not then your configuration is wrong. If you have xampp then make sure it is in the HTDOCS directory

Apache: Inline PHP not working on Linux

I am having trouble getting my PHP to work for my Apache server. I am running Oracle Linux, and used yum install php + yum install httpd to get my PHP and Apache. I have scoured the internet and done a couple of things:
Firstly, I have gone into my httpd.conf file and added LoadModule php5_module modules/libphp5.so. I have chosen to use the one in my php.conf file, which is the exact same one. Furthermore, I have done AddType application/x-http-php .php, so now my .php files are loading fine.
Secondly, I have tried to run my Apache server, and it works fine. It displays my index.html file, which I set with DirectoryIndex. However, when I tried to put php code into it, it got automatically commented out, which I assume is because I haven't set it to properly execute on the server side.
Now, with all that said, I am wondering what else I may be missing. I have two set-ups, one where I have a .php file, and one where the php is inline with the html.
index.html
<!DOCTYPE html>
<body>
<?php echo "Hello everybody."; ?>
</body>
</html>
index.php
<?php phpinfo(); ?>
The index.php file loads fine, but the index.html doesn't run the php code. How do I get the inlined version of php to work?
Note: I have set-up my Mac OS to work fine with .php files, but it is also having trouble with inline php within an Html file. What am I missing?
SOLUTION: Html files themselves cannot include php. Instead, the file must be a .php extension, and within a .php file, you can have text, html, and JavaScript.
I do not think .html files are run through php at all so the <? ... ?> is treated as a normal tag which is invisible.
Change the extension to .php.
Php can contain html.
Edit:
There exists an option to configure for example Apache to parse html files as if they are php if you for some reason cannot or do not want to use php endings.
This, in my opinion, is not a good solution as it hides the fact that the page is dynamic to a future maintainer.
There is a similar question which has a similar problem:
PHP code is not being executed, instead code shows on the page
You might want to check out points 2 to 5 in the accepted answer:
LoadModule (It seems, that you have configured that properly)
Set Apache to run PHP files (this is the third point, and it seems that
you didn't configure that). Add the following line to your httpd.conf file: AddType application/x-httpd-php .php
Make sure, that you have the file ending with the .php extension (after you have configured Apache to run PHP files, see the previous point)
Change your code to use the long PHP opening tag (<?php instead of just <?)
You need to set short_open_tag = On in your php.ini file

error integrating PHP into HTML

I got a forum in PHP and I got a website in HTML
I need to utilize the website template "to unify the appearance" to output the forum contents
I got stuck when I wanted to include the PHP code into the html file
I used the
<?php echo "here goes my PHP code" ?>
but it does nothing to me :(
so can I get some help to move forward in my development
I have tried and get tired of Posting the code piece here so I decided to post the question and try to figure out the code posting later if needed
thanks anyway
If it "does nothing" then the web server (if there is one) is probably passing the file "as-is" to the browser, and the browser is interpreting it as an unknown tag.
You must:
Access the page through a web server
Be using a web server that supports PHP
Have the web server configured to treat that file as PHP (by default, on most systems, this is done by giving the file a .php file extension)
wanted to include the PHP code into the html
HTML is usually not processed by PHP. Only files (in default configuration) that end with *.php are passed thru PHP interpreter. If these HTML files you are willing to alter are standalone files (and you access them directly like http://domain/file.html), then you can try to enforce PHP there. If your server is Apache and you can tune its settings with .htaccess file (or if you can alter main Apache's configuration) then you can try adding this to your .htaccess
<Files "*.html">
ForceType application/x-httpd-php
</Files>
If you are aiming at HTML templates then basically you are out of luck as these files are not going thru webserver but are read by i.e. your forum and usually processed (not executed etc) inside the code. Slight chances are that forum uses i.e. Smarty templating engine and you can tweak the forum code to enable PHP in Smarty templates.
EDIT
For PhpBB .htaccess won't do the trick, as these HTML files are plain templates. However you can enable PHP in templates in Security Settings. Just set "yes" to "enable PHP in templates". Once you do that you can use PHP code, but while PHP code itself is the same, PHP blocks are marked differently than usual <?php and ?>. Instead of
<?php echo "Test"; ?>
you need to wrtite
<!-- PHP --> echo "test"; <!-- ENDPHP -->

Browser ask me to download php file

I am a new guy in web programming who still learning new stuff.
I am creating a web form that consist of one INPUT. I connected the form in the HTML file to the php file. I am using a localhost MAMP that I recently installed on my MacBook.
The Problem : When I open the HTML file on a webpage and type something on the input box and hit submit, the browser ask me to download the php file rather processing it.
I don’t think the problem is related to mysql or apache since when I run the MAMP it give me green signs next to the apache and mysql.
Do anyone know how I can fix this problem and the let the php file open and transfer inputs to the database?
You'll want to make sure that Apache has been told that .php files should be treated as PHP scripts. That means one of the following:
LoadModule php5_module modules/libphp5.so # on windows, this'd be a .dll instead
AddHandler php5-script php
and/or
AddType application/x-httpd-php php
in your httpd.conf file.
open your file from virtual web-server, not filesystem.
you have to type something like
http://localhost/form.html
in your browser's address bar
Did you checked out http://www.mamp.info/en/documentation/faq.html#q8 ?
Maybe you didn't put your site into the folder "htdocs"?
PHP won't be parsed unless it is located in that folder or a subfolder.
That would explain the fact that your browser downloads the php file instead of getting the parsed php from apache.
This error is not only about php but general. The file that the browser suggets to down load is simply the script name, php or not.

Categories