PHP Forms and User Input

The example below contains an HTML form with two input fields and a submit button:
1
2
3
4
5
6
7
8
9
<html>
  <body>
  <form action="welcome.php" method="post">
  Name: <input type="text" name="fname" />
  Age: <input type="text" name="age" />
  <input type="submit" />
  </form>
  </body>
</html>
When a user fills out the form above and clicks on the submit button, the form data is sent to a PHP file, called "welcome.php", "welcome.php" looks like this:

1
2
3
4
5
6
<html>
  <body>
  Welcome <?php echo $_POST["fname"]; ?>!<br />
  You are <?php echo $_POST["age"]; ?> years old.
  </body>
</html>

Output could be something like this:

Welcome John!

You are 28 years old.

Notice that any form element in an HTML page will automatically be available to your PHP scripts.
PHP If...Else Statements
PHP For Loops
PHP Functions
PHP introduction