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).
<form method="post" action="">
Name: <input type="text" name="name" />
Email: <input type="text" name="email" />
How did you hear about us?
<select name="hear_about">
<option value="1">Radio</option>
<option value="2">Television</option>
</select>
<input type="submit" value="Submit" name="action" />
</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.
if(isset($_POST['action']) && $_POST['action'] == 'Submit')
{
if(isset($_POST['name']) && strlen($_POST['name']) > 0)
$name = $_POST['name'];
if(isset($_POST['email']) && strlen($_POST['email']) > 0)
$email = $_POST['email'];
if(isset($_POST['hear_about']) &&
($_POST['hear_about'] == 1) || ($_POST['hear_about'] == 2))
{
$hear_about = $_POST['hear_about'];
}
if(isset($name) && isset($email) && isset($hear_about)
{
mail(
'your-email@something.com',
'New contact submission!',
"{$name}\n{$email}\n{$hear_about}");
$form_success = true;
}
else
$form_success = false;
}
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.
Comments (0)