A PHP Contact Form for your Site
Websites commonly require a contact form that allows site visitors to type a message that is then emailed to the site owner. For a “front end” web designer (who deals with things like HTML, CSS and the like) it’s easy enough to create the HTML form. But to get the form data entered by the visitor to actually be sent to the site owner, a “server side” script of some kind is needed.
A common server side language for processing form data is PHP. There are many different ways to write and set up PHP scripts for this purpose. I’ve made up two simple form processing scripts that you can use on your website. Which you use depends on your setup.
Please note that both scripts include a “honeypot” field that prevents spam bots from filling up your inbox. Likewise, each script includes security features that prevent spammers and bots from inserting malicious code into your form.
Internal Script for PHP-based Contact Pages
If your Contact page (the page that contains the contact form) has a
.php extension (such as
contact.php), the script below is an option for you. Simply download the code and place it in your Contact page. You will also need to create a Thank You page. (Instructions are included in the download.)
External PHP Script for HTML-based Contact Pages
If your Contact page ends with a
.html extension and has been online for a while, you may prefer not to change the extension to
.php. Any links to the
.html page (such as those stored by Google) may give an error if you change to a new page—although you can fix this with a
So this alternate script runs externally to the Contact page, but does pretty much the same thing as the first script. Simply download the code and place the script page in the same folder as the Contact page. You will also need to create a Thank You page. (Instructions are included in the download.)
- There are various tips for setting up your Contact page in the download files above, but something else to consider is the format of your page URLs. It’s much nicer for your contact page URL to appear as
mysite.com/contact/rather than something like
mysite.com/contact.html. Here is a simple tip on making your URLs look nicer.
- Of course, before you can use this form processing script, you need to ensure that your server actually has PHP installed … although it’s a rare host that doesn’t.