Dealing with Forms: A simple HTML form

<form action="action.php" method="post">
 <p>Your name: <input type="text" name="name" /></p>
 <p>Your age: <input type="text" name="age" /></p>
 <p><input type="submit" /></p>
</form>
Hi <?php echo htmlspecialchars($_POST['name']); ?>.
You are <?php echo (int)$_POST['age']; ?> years old
  • When the user fills in this form and hits the submit button, the action.php page is called.
  • htmlspecialchars() makes sure any characters that are special in html are properly encoded so people can't inject HTML tags or Javascript into your page.
  • For the age field, since we know it is a number, we can just convert it to an integer which will automatically get rid of any stray characters.
  • The $_POST['name'] and $_POST['age'] variables are automatically set for you by PHP.

Related concepts

A simple HTML form

Dealing with Forms: A simple HTML form — Structure map

Clickable & Draggable!

Dealing with Forms: A simple HTML form — Related pages: