This is a very simple and user-friendly guide on how to add a PHP contact form to your website. Even if it says "create an online contact form in PHP", you don't need any knowledge of the PHP programming language to be able to do this.
As you probably noticed, contact forms have become one of the most important piece of a website; blogs, corporate websites, personal pages, e-commerces, etc, all have one nowadays. It is really useful to contact directly a company or a person without having to use a mail application of service. It is also safer than the use of a "mailto" hyperlink in your page, which has become the easiest target of spammers.
People that simply want the files used in this tutorial can find them here.
In this tutorial, we are going to use three separate files to build our contact form.
(These three files could be combined in one big PHP page, but for this article I'm goin to use separate files to keep everything clean and simple.)
- contactUs.html - This is a simple HTML page that is going to be used to collect the data from the user.
- contact.php - The PHP script that will handle and treat the data entered in the HTML page.
- confirmation.html - The HTML page we want to display once the email was sent.
One thing you should know before starting is that not every hosting service supports third party scripts for emails or anything else for security reasons. Personaly, I had GoDaddy as host for some time and scripts were not allowed. For people with the same problem, you need to use the PHP script offered by GoDaddy. For more information.
Let's start with the file "contactUs.html". A simple HTML page with a basic contact form:
Note that this is only the code for the form. You could use a CSS in your page to style the fields and make it good-looking.
Let's examine this code and explain exactly what it does.
You can see that the first attribute of the form is the 'method' which is set to 'post'. Post is a submission method that unlike the Get method, doesn't encode the information gathered in the form to a URL (or URI) to send them to the server. Also, in our form it will be safer to use the post method because it supports non-ASCII characters and can send request with thousand of characters (the method="get" may cause practical problems with implementations which cannot handle that long URLs).
When the form is submitted, the data is sent to the destination specified in the form’s action attribute, and contact.php page is loaded.
You may notice that I use a fieldset in my form; this is a pratical way of grouping items in a form. The text inside the legend tag is displayed at the top of the fieldset.
The inputs in the form are the fields the user will have to fill. The size attribute is the width of the field. As for the textarea, it is simply a big multiline textbox.
The first part is done and the user as filled the entire form and clicked the Submit button. As explained before, the information entered by the user is sent to the contact.php file via a post request.
Those of you who don't know a thing about PHP don't have to panic. This is a simple script that simply reads data and there's only one thing to change in this file: the destination email address. That's all.
But first, let's then take a look at what's inside the PHP file:
This PHP script does 4 things:
1- It takes the data sent by the form, cleans them to protect you from spammers and hackers and finally put them into local variables.
2- The script validates the data and returns an error to the user if some fields weren't filled.
3- It builds the email's content and send it to the destination email address.
4- Then it redirects the user on a confirmation page is everything went ok, or prints an error message.
*** The only thing that you have to change in this script is the destination email address ***
This is the line you need to change
$EmailTo = "youremailaddress@domain.com";
To make it work, simple change the "youremailaddress@domain.com" to the address you want the server to send the email.
Note that you could also use a static Subject by changing the following line:
$Subject = clean_data($_POST['Subject']));
to something like this:
$Subject = "My Website - New Message";
The last thing you need to do is to give a feedback to the user once an email has been sent.
In this guide, when the process is complete and there was no error, we redirects the user on the confirmation.html page.
This could be changed in the PHP file to simply print a confirmation message on the page.
The only thing you need to do is create a new page named "confirmation.html" and put it in the same directory on your server then the two other files.
That's it, you online contact form is now ready to be used. If you have any questions regarding this tutorial, feel free to contact me.
You can download the files used in this tutorial here:
- Frank P. (ExodeCreations.com)
Comments
Post new comment