Irwin Associates Web Design; modern technology, traditional values

A better PHP contact form

contact form

There are dozens of tutorials for creating contact forms. However, on inspection, manymost of them either use outdated code or methods (for example, using tables for layout, or no label tags), or only do half the job. So I decided to create my own. The purpose of this script is to provide a fairly basic web response form whereby a message can be sent to one of several people, such as members of a committee.

The form employs responsive design and utilises PHPMailer rather than the PHP mail() function for sending the email. (Although PHP mail() is simpler to code initially, it is less reliable, and is even disabled on some hosts.) The form comprises a single PHP file, plus CSS, and utilises a list of recipients held in a CSV file, contacts.csv, for example:

"Ms Pen, Clerk",[email protected]
"Mr Standing, Chairman",[email protected]
"Mrs Sterling, Treasurer",[email protected]

The message includes a character limit and a JavaScript character countdown, which can be changed or removed if necessary, and of course the select element may be removed if there is only one intended recipient. Additional fields can also be added to the form if required.

You can test-drive a demo of the form. Rather than give a lengthy explanation as to how it works, you can view and/or download the source code and play with it. If you save the source code, you will need to change the extension to php before trying to use it. You can also view/download the source code of an alternative version without the select box but with a simple CAPTCHA system. Of course, the CAPTCHA option can be removed if it’s not wanted. Both versions include a simple technique to help hinder bot submissions.

This form works but could certainly do with some improvement. For one thing, it needs updating to the latest version of PHPMailer. Any comments or suggestions for improvements would be welcomed via our contact form.

Last updated 2018-09-13.