Notes

Handling data from HTML forms

Edit on GitHub

PHP

Here’s an HTML form

1<!-- ./index.php -->
2<form action="contact.php" method="post">
3  <label for="contactName">Name </label>
4  <input type="text" id="contactName" name="contactName" />
5  <input type="submit" value="Submit">
6</form>
  • action attribute decides where the data goes, usually another file/page. (You can submit it to the same page as well if you’re going to process it on the same page. In this case you won’t be redirected to another page)
  • method attribute decides the HTTP method/verb
  • name attribute is what you’ll use to access this data with $_POST
  • the id of an inout is used with the for of a label to associate the two together

Here’s the PHP page you’ll send the data to

1<!-- ./contact.php -->
2<?php
3	$name = $_POST["contactName"];
4	echo "Hello " . $name;
5?>
  • the posted data is accessed with a special variable called $_POST. You access posted values with their name.