简体   繁体   中英

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

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

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 ie '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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM