(519) 282-8272
GO WHERE YOU WANT. let me help you get there.

Super Easy Contact Form Handler

by Chad Tiffin

Super Easy Contact Form Handler

Probably the most common “back-end” work I do when building websites is setting up a simple contact form, and often, that’s the only server-side code a website needs (aside from any CMS related stuff or template includes). 

It’s a mundane process to have to write a new handler script for every variation of a form, so I wrote a simple PHP script can handle the input from any front-end variation of a contact form. This basically cuts my work in half, as now I only have to worry about writing the HTML for every new contact form, and this script can process it and deliver it to a set of recipients no matter how the fields are set up.

Of course this is a great option for static websites where we don’t really have any plugins or tools to help us build a contact form, but its great for CMS websites too. CMS websites usually have some sort of plugin that can handle this functionality, but especially with Wordpress websites, the fewer plugins you need to the use, the better. If you only have a simple need to take some information and mail it to an email address, why use a plugin? We can do it ourselves!

My own implementations have some extra checks for special fields (like a reply email address), logging, some extra CSS for styling the HTML email, etc. I’ve removed most of that extra stuff to just show a simplified version of the script so its a little easier to read through and understand. One thing I did keep in there however is a check for a honeypot field, which only processes the form if that honeypot field is empty. Although its not a perfect spam solution, I do find it cuts down on a huge amount of spambot form submissions, and its an easy thing to do.

If you find this script useful, feel free to use it!

Of course if you want to get fancy this script will also work just fine with AJAX requests (which is my preferred method), you’d just likely want to change the success message to respond with some JSON rather than an HTML message.
It might also be a good idea to add some logging of requests, both the successful ones and the ones that get rejected by the script, either by appending into a simple text file, or writing to a database. But for now what we have above is a very simple, and very extensible “just works” starting point.

For those who’d like to see an example of what a form might look like, here’s a quick HTML form that you might use in conjunction with it:

NOTE: The main thing with input fields is you need to include a “name” attribute, which is what creates a field that goes to the server.

Did you find this useful or do you have your own implementation? I’d love to see what other people are doing.

Categories: PHP & MySQL Tips, Web Development,

Chad Tiffin

Chad Tiffin

In addition to running his own freelance web development company, Chad often works on long-term contract for companies in need of an in-house developer for ambitious software projects.