1

I'm very new to php/html, and I'm trying to teach myself the basics of creating and processing an html form.

I created a folder called Website. In it I created an html file index.html. I also created a file submit.php.

(this code is taken from: http://www.w3schools.com/php/php_forms.asp)

In index.html I have:

<html>
<body>

<form action="submit.php" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>

</body>
</html>

In submit.php I have:

<html>
<body>

Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>

</body>
</html>

When I open the html file in chrome and fill in the blanks and press submit, I get redirected to a page with the code in submit.php:

<html>
<body>
Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>
</body>
</html>

I should be getting this output:

Welcome Hannah
Your email address is Hannah@example.com

What am I doing wrong that the output isn't working? Thanks!

  • 2
    It seems like you don't have a webserver installed... – Henrique Barcelos Nov 15 '13 at 02:21
  • What output do you get? Or do you not get any output at all? – randomusername Nov 15 '13 at 02:21
  • Side note, for when you get it working. You should use [htmlspecialchars](http://php.net/manual/function.htmlspecialchars.php) when outputting user input, for security reasons. Check the answer [here](http://stackoverflow.com/questions/129677/whats-the-best-method-for-sanitizing-user-input-with-php). – Matthew Johnson Nov 15 '13 at 02:27

2 Answers2

2

PHP is processed on a server, so you can't treat it like HTML, it needs to be placed on a server that has apache installed. You can install one on your computer, using something like the following:

Arian Faurtosh
  • 17,987
  • 21
  • 77
  • 115
  • So how do I go about installing a server so that this works? This is likely a dumb question, but I really don't have any background at all! Thanks. – user2994585 Nov 15 '13 at 02:28
  • @user2994585 what OS are you running, Mac, Windows, Linux? – Arian Faurtosh Nov 15 '13 at 02:30
  • 1
    @user2994585 then just download the [mamp](http://www.mamp.info/) server and install it, be sure to put your files in the `htdocs` – Arian Faurtosh Nov 15 '13 at 02:34
  • Just did that -- downloaded MAMP and put the two files (index.html and submit.php) in the htdocs file. Then I went to chrome and opened index.html, filled out the form and hit submit. Same thing, not getting the right output... – user2994585 Nov 15 '13 at 02:36
  • Did you start MAMP and load the page within a web browser going to the main MAMP page URL of “localhost:8888?” – Giacomo1968 Nov 15 '13 at 02:44
  • Yes, I did, and I have two green lights for both servers. What do I type into the URL slot of Chrome/Safari/etc in order to properly open the file? – user2994585 Nov 15 '13 at 02:51
  • `localhost:8888/index.php` or `localhost/index.php` I don't know which one on a Mac, I never used it... – Henrique Barcelos Nov 15 '13 at 02:52
  • It should just be `localhost:8888/` if it is an `index.html` or `index.php` page. – Giacomo1968 Nov 15 '13 at 02:53
1

Sounds like you are doing this via files on your desktop and not via a web server. Either on the Internet or via your local machine. I took your example 100% as presented, placed it within my htdocs folder in MAMP (LAMP for the Macintosh) and it behaves 100% as expected.

The difference between loading files on your desktop versus a web server is a web server will process the PHP. If you just do it as files, then the file gets loaded & doesn’t get parsed.

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
  • It could be that it is cached in Chrome. I don’t use Chrome because it caches content too well & gets in the way of web development. I recommend you open the same MAMP URL of `localhost:8888` in another browser or clear the cache in Chrome & reload the page. – Giacomo1968 Nov 15 '13 at 02:52