hypGnosis
hypGnosis

Reputation: 1

php script works fine, but not inside HTML

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

Upvotes: 0

Views: 545

Answers (3)

Norlihazmey Ghazali
Norlihazmey Ghazali

Reputation: 9060

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.

Upvotes: 1

Koala Yeung
Koala Yeung

Reputation: 7843

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.

Upvotes: 1

Amit
Amit

Reputation: 463

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.

Upvotes: 0

Related Questions