Bernard
Bernard

Reputation: 4580

Why php tag is not working in html file?

I have a register.php file and I have defined $name="" inside that file

global $name;
$name = "";

and now in the html form I have:

    <form name="register" action="register.php" method="post" class="smart-green">


        <h1>Contact Form
            <span>Registration Form</span>
        </h1>
        <label>
            <span>Username:</span>
            <input id="name" type="text" name="username" value ="<?php echo $name;?>" placeholder="Enter your user name" maxlength="20" />
        </label>
   </form>

But the output is <?php echo $name; ?> instead of empty! Any idea of how I can fix this?

Upvotes: 4

Views: 51595

Answers (4)

Andrei Krasutski
Andrei Krasutski

Reputation: 5257

If you are using apache2, you can specify a site specific /etc/apache2/sites-enabled/MySite.conf

<VirtualHost *:80>
...
  <IfModule mime_module>
     AddType application/x-httpd-php .php .php5 .php7 .phtml .html
  </IfModule>
...
</VirtualHost>

Open your terminal window and make sure the module is enabled:

$  a2enmod mime

Reload the configuration:

$  systemctl reload apache2

Done

Upvotes: 2

Suku
Suku

Reputation: 13

That right, for the file to be recognized as php it's extension should be ex- form.php that way it can be executed to give the output

Upvotes: 0

user1720897
user1720897

Reputation: 1246

Your web server will server the HTML page as is. It will only parse the HTML as best as it can. If you rename your page with a PHP extension, the web server will parse it using the PHP interpreter and that is when PHP will be interpreted. Also as Fred points out in the comments you can tell Apache to treat HTML as PHP.

Upvotes: 5

Aris
Aris

Reputation: 5057

rename the form file to '.php' in order for php tag to be interpreted.

In a php file you can have both php and html code, but not the other way around.

Upvotes: 1

Related Questions