Martin Laing - Multimedia Developer | Fife, Scotland

How to create a Contact Form using PHP

How to create a Contact Form using PHP

This tutorial aims to show you how to make a Contact Form for your website, which will send the completed form results to a specified email address. This tutorial only explains the basics and doesn't go into Captcha or Form Security (I will create a future tutorial to deal with this and other spammer/robot related issues, as these are too complexed to explain all in one tutorial.)

Step1 - Setting up the Form page

First of all we must setup the page which will display the HTML Form. I'm just going to setup a simple html form to send a simple message and to a site owner, but this can be extended to include more advanced form items such as radio buttons etc for surveys and things.

So the form code should look like this:



<form action="contactme.php?action=sendmessage" method="post" enctype="multipart/form-data" name="sendmessage" id="sendmessage">
<label>
<div align="left">Your Name:
<input name="name" type="text" class="form" id="name" />
</div>
</label>
<label>
<div align="left">Email Address:
<input name="email" type="text" class="form" id="email" />
<br />
Reason for Contacting:
<input name="rfcontact" type="text" class="form" id="rfcontact" />
</div>
</label>
<div align="center"><br />
</div>
<label>
<div align="left">Message:<br />
<textarea name="message" cols="50" rows="5" class="form" id="message"></textarea>
<br />
<input name="send" type="submit" class="submitbutton" id="send" value="Send" />
</div>
</label>
</form>




If you are already familiar with HTML/XHTML then the above should be easy for you if not heres a little explanation as to what it does:



<form action="contactme.php?action=sendmessage" method="post" enctype="multipart/form-data" name="sendmessage" id="sendmessage">


The above code is what we call the form wrapper all the form items should be kept within this tag.

The Form Tag

The form tag has has the following parameters:

- action - This is just where the submitted querystring will get sent to i.e your php form handler file.(In this case it's the same page as i'm using an if else php condition to determine what state the form is in.

- name and id- This one is selt-explanatory it's simply the name you want to call the form keep both name and id the same!!

- method - This is the way the form will send the contents.There are two types: GET and POST. You would always use POST when sending sensitive data such as passwords as the contents are not sent as a querystring in the URL. with GET they are sent in the URL as a querystring and is fine for things like search terms and things.

- enctype - This is the type of encoding the form should use i find multipart/form-data usually works fine. If you want an in-depth explanation of the different type use the contact me link on the left and i'll email them to you.

Ok now we've got the form tag out the way lets move on to the php handler part of the script as this tutorial isn't meant to teach you everything about html forms but how to use them as contact forms. If you would like more information about HTML forms please use the contact me link on the left to message me and i'll send you information about them.

Step 2 - The PHP Handler

Now this is the trickier part of contact forms, so pay attention (hehe). Basically what we are going to do is take the form contents and send them off to the owner of the site, sound simple enough, well really it is you just need to learn how to do it thats all.

I've just put the whole PHP handler file code below and added comments to tell you what the code does. (Comments are lines which start with // ).



<?php
// always use <?php to start sections of php code

// lets check to see if the form has been submitted
$action=$_REQUEST['action']; // this gets the action variable which we set in the action form parameter

if($action=='sendmessage') {

// lets get the variables now
$email=$_REQUEST['email']; // this gets the email content
$name=$_REQUEST['name']; // this gets the name content
$rfcontact=$_REQUEST['rfcontact']; // this gets the reason for contact content
$message=stripslashes($_REQUEST['message']); // this gets the message content, i will explain stripslashes later in the tutorial.

//ok we've got all the content lets send the email to the site owner. Note i will explain the different parts of the mail() function later in the tutorial.
$header = "From: $email\n"
. "Reply-To: $email\n";
$subject = "$rfcontact\n";
$email_to = "siteowner@sitename.com\n";
$message = "Dear Site Owner,\n\n"
. "$message\n\n"
. "Regards,\n"
. "$name\n"
. "$email\n"
."------------------------------------------------------------------------------";
$mail=mail($email_to, $subject ,$message ,$header ) ;
if ($mail==0) {
echo"There was a problem sending the message please try again later!";
} else {
echo"The message has been successfully sent i will try to email you back as soon as possible ".$name."!";
}

} else {
// the html form will be put here in the full code
}

// we also always end php code blocks with ?>
?>




Ok so that's the PHP Handler, note the handler and form can be put in different files but i feel it's much smarter to keep it all in one file and use if else conditions,if your not sure what they are then i will explain them to you:

IF ELSE Statements

They are basically a way of mapping out logic in php, think about it in your head IF form has been submitted then send email ELSE show the form. Makes sense doesn't it! Well the basic way it works in php words is if(condition==statement) { action } else { action }.

If the above doesn't make sense to you or you want me to clarify any of it then please use the contact me link on the left to let me know and i'll do my best to help you.

Ok so now you want to know what the stripslashes thing is all about, right?

Stripslashes()

all stripslashes does is scans through the variable, in this case $message, and checks to make sure that any slashes (/ or \) have been removed from the contents. To explain why this is necessary is because when you send a message from a form you sometimes add things like ( ' ), inadvertently ofcourse when using words like (can't, won't, don't etc..), well PHP has a funny thing where it changes those charecters to a slash instead and that doesn't look very good to the viewer. So stripslashes takes care of that and returns it back to the original charecter. How good is that eh!!

Ok so now you want to know the different parts of the mail() function.

mail()

The mail() function comes with the following parts to make it work properly:

- $email_to - This is where the email address of the receiver should be entered, in this case the site owners email address.

- $header - This is where the header content of the email should be put (i.e the senders email address and the reply to address.)

- $subject - This is quite simply the subject of the email.

- $message - This is the contents of the message, sometimes referred to as the message body.

That is all that is required to send the email, the code i am showing you how to make includes error checking, which basically means it will check to see if the mail() function has returned true or false, this is in no way an absolute indication the email has been sent, but lets you know the mail function has worked.

The error checking code and the part which sends the message are as follows:



$mail=mail($email_to, $subject ,$message ,$header ) ; // this is the mail function which is wrapped in a variable for error checking purposes, this is the line of code which sends the email.

// the following IF ELSE condition checks if the mail function returned a true or false value.
if ($mail==0) { // this means if the mail function returns 0 (false) then
echo"There was a problem sending the message please try again later!"; // display this text as an error
} else { // else
echo"The message has been successfully sent i will try to email you back as soon as possible ".$name."!"; // show this message as confirmation the process was successfull.
}




Ok so that wraps up the tutorial all thats left to do is show you the full code, well here it is: click here to view the full code

Comments

Comments will be displayed here

Back to Tutorial Home

Latest Projects

Fallen Angels Website Development Bucketball Candy Drop Fallen Angels Bebo Application Hector the Inspector

View Full Portfolio


Contact Me

If you would like to contact me, the easiest and quickest way to do so is by filling out the contact form on this website.

Send Me a Message

If you prefer you can contact me on any of these Websites:


Bebo facebook Myspace Twitter

Tutorials


How to send data from Flash to PHP How to create a Contact Form using PHP How to create simple Member Login for your websites. Part 1 - Member Login How to create simple Member Login for your websites. Part 2 - Session Management How to create simple Member Login for your websites. Part 3 - Logging Out Users