Basic Forms with PHP

HTML forms are a great way to collect information directly from your users. There are plenty of input options available, even for the most basic, non-javascript enhanced forms. This post will go over the steps for creating, validating, and handling an elementary contact form with PHP and HTML.

We'll have to make a few basic assumptions before beginning. First, we'll assume that the page the form is displayed on can be executed with PHP. This could mean that either the page has a 'php' extension or the web server is configured to run this file as PHP, regardless of the file extension. Second, we'll assume that there is no conversion tracking (that is, analytics tracking the number of page visits vs the number of form submissions). This will make our submit logic a bit simpler. Finally, we will not be doing any javascript or ajax trickery. All the fields and submit request are handled as browser defaults.

Our initial step involves setting up a basic html form. We'll be submitting via the 'post' method, which passes the form parameters directly in the request (not within the url string).

  1. <form method="post" action="">

  2. Name: <input type="text" name="name" />

  3. Email: <input type="text" name="email" />

  4. How did you hear about us?

  5. <select name="hear_about">

  6. <option value="1">Radio</option>

  7. <option value="2">Television</option>

  8. </select>

  9. <input type="submit" value="Submit" name="action" />

  10. </form>

Besides signifying the 'post' method inside the html element, you also have to define where the form submission goes within the action element. While you could have it submit to a separate URL, it is much easier just to have the form submit to itself. Now we can place the form submission logic at the top of the this file instead of tucked away somewhere else.

  1. if(isset($_POST['action']) && $_POST['action'] == 'Submit')

  2. {

  3. if(isset($_POST['name']) && strlen($_POST['name']) > 0)

  4. $name = $_POST['name'];

  5. if(isset($_POST['email']) && strlen($_POST['email']) > 0)

  6. $email = $_POST['email'];

  7. if(isset($_POST['hear_about']) &&

  8. ($_POST['hear_about'] == 1) || ($_POST['hear_about'] == 2))

  9. {

  10. $hear_about = $_POST['hear_about'];

  11. }

  12. if(isset($name) && isset($email) && isset($hear_about)

  13. {

  14. mail(

  15. '',

  16. 'New contact submission!',

  17. "{$name}\n{$email}\n{$hear_about}");

  18. $form_success = true;

  19. }

  20. else

  21. $form_success = false;

  22. }

Since the form is being submitted by the 'post' method, all of the form elements are passed within the php $_POST superglobal. You can grab the value of each submitted value by accessing the name of the element as a key in the $_POST array. The validation is simple in this example - only checking to make sure that the post parameter was sent to this code and has a string value. You can easily add more complicated checks for the structure of the email input, length of the name, etc.

You may have noticed that this script sets a variable called $form_success. It could actually have three separate values: unset, true, or false. If the value is unset, then you would just display the form. If the value is true, hide the form and show a thank you message. If the value is false, show the form and show an error message.

And that's how you create a very basic form with simple validation. In the next post we'll go over some best practices for forms, digging deeper into the thought process behind the handling and behaviors.