Tutorial – Simple PHP/XHTML Contact Form

This tutorial will show you how to build a very simple contact form. Keep in mind while this form will function it has no security checks built into it, the goal of this tutorial is just to show you how a basic contact form works.

Now lets start with the XHTML form, it is fairly basic but I will break it down just in case you need a refresher:

< br /> < br /> < br />

Note: the break tags have a space added to them so they show up in the coding. That is all the code that is required in the XHTML portion, now lets break it down section by section.

This line is going to use the method=”post” to send the data added through the input/textarea fields within the form tags. That data will be sent to the action=”contact.php”.


This adds a label (in this case “Name:”) to the input field of name=”name”. value is left empty so there is no text inside of the input field when the page loads. It is given a size of 26 just to stretch the field out a bit, you could actually give the input an id or class and control it through CSS if you want a more uniform method to control it.

Textarea is a bit different than input fields in the sense that it supports multipe lines of text as well as supports the enter key being pressed for a line breaks.

This is the Submit button which will send the filled out information via the post method to execute the actions defined in contact.php.

Now Lets move on to the contact.php code. I will break down the code by section but to give you an idea of what it will look like when we’re don here is all the code:

Now lets break this down by sections so you will have a better understanding of what it is doing.

$name=$_POST['name'];
$email=$_POST['email'];
$message=$_POST['comments'];

This part of the code is getting the information from the XHTML form we just created. It then assigns that information to their respective variables, which we’ll be using shortly.

$email_to = 'enter your email here';
$headers = 'From:'.$email;
$email_subject = "You have an email";
$email_message = "Form details below.\n\n";
$email_message .= "Name: ".$name."\n";
$email_message .= "Email: ".$email."\n";
$email_message .= "Comments: ".$message."\n";

This part of the code is defining the message that will be sent to you when someone hits the submit button. Now I’ll go over this line by line to prevent any possible confusion/questions.

$email_to = 'enter your email here';

This is where your email goes.

$headers = 'From:'.$email;

This will send the email from the email address of the person who filled out the form so you can just hit reply and send them a response.

$email_subject = "You have an email";

The subject line of your email.

$email_message = "Form details below.\n\n";
$email_message .= "Name: ".$name."\n";
$email_message .= "Email: ".$email."\n";
$email_message .= "Comments: ".$message."\n";

This sections is posting the information from the form with a bit of extra code to make it easier to understand each part of information being sent to you.

The last part of the code will actually send out all of the information contact.php has just gathered and formatted.

if(isset($_POST['email'])) {
	@mail($email_to, $email_subject, $email_message, $headers);  
	header("Location: http://www.yourwebsite.com");
}
?>

This is checking to see if $_POST[’email’] has been set, in other words if the form has been filled out. Now I’ll break up the @mail so that it is easier to understand what it is doing. This is what it is doing:

@mail(who to send the email to, the subject of the email, the message of the email, the header information like from/cc/bcc)

The line that starts with “header(” is simply defining a website to redirect the browser to once the form has been processed.

Now that you’ve completed the contact.php file save it and place it in the same location as the page you added the XHTML code for the contact form. If you want to have a different location for the contact.php file make sure you change action=”contact.php” to reflect the new location.

That is all you need to create a basic contact form using PHP and XHTML.

Leave a Reply