Why php tag is not working in html file?

2020-04-19 08:46发布

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?

标签: php html forms
3条回答
叼着烟拽天下
2楼-- · 2020-04-19 08:51

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.

查看更多
Root(大扎)
3楼-- · 2020-04-19 09:08

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.

查看更多
Viruses.
4楼-- · 2020-04-19 09:09

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

查看更多
登录 后发表回答