Simple Contact Form With PHP
In this post I'm going to teach you how to build a simple contact form using PHP. This post does not cover form verification or headers, but I plan on writing a few articles in the future about these topics. For now we'll just build a functional form with php integration.
The first thing that we'll need is a simple form. Lets use the following:
<form action="contact.php" method="post">
Name: <input name="name" size="50" type="text">
<br />
E-mail: <input name="email" size="50" type="text">
<br />
Subject: <input name="subject" size="50" type="text">
<br />
Message:
<br />
<textarea rows="20" cols="50" name="message"></textarea>
<br />
<input value="Submit!" type="submit">
</form>
Simple enough eh?
I'm going to assume that you know the different parts of the form. Just as a refresher know that the action property tells what page to send the browser to to process the form, and the method property tells you how the variables will be sent to the server. You can use either the "get" method or the "post" method. The main difference between the two is that the "get" method will include the variables in the URL in the address bar, and the "post" method will not. GET methods are typically good in inventory systems where you would like a user to be able to bookmark a specific item, while POST methods are better for form usage.
Anyway, back from one of my tangents. Save that form in whatever file you'd like and send it up to your server.
Now start a new text file and go ahead and save it as contact.php. This is going to be the file that processes the form, and mails the message to the user.
First the code, and then the explanation:
<?php
// Step 1
$name = $_POST['name'];
$subject = $_POST['subject'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = "akozlik@gmail.com"; // The e-mail address you want the message sent to
// Step 2
$header = "From: " . $name . " < " . $email . ">";
// Step 3
mail($to, $subject, $message, $header);
// Step 4
header("Location: thankyou.php");
?>
Step 1:
Set a few variables and get their values using the $_POST prefix. If you used the "get" method on your form replace this with $_GET['name'] etc . . .
Step 2:
I know I said that I wasn't going to put in headers, but I felt this is a pretty good one to include in this tutorial. I go more into headers on a different post, but know that this one is great to have. The header variable winds up with the form "From: Andrew
Step 3:
The meat and potatoes of this project is PHP's mail() function. This is setup in the format mail(to, subject, message, header). Notice that there is no "from" parameter. This is why I included the From in the header, as that's how the mail servers read that information. We simply include our variables and let our sendmail server do the rest.
Step 4:
After mail is sent, you can then redirect the user to a thank you page. Remember that this header() function must be used before any text is displayed to the browser. If you use any echo statements or straight HTML before this point, the redirect will not work and you will get an error.
Well, that's about all I have to say on the topic of simple forms. Hopefully somebody finds this to be a useful article and is able to get some work done thanks to it. As usual, I'll put my shameless plug in here and ask that you click a few ads before departing, if you like the article. Better yet, subscribe to my RSS feed located to the side of this post. Thanks for reading, and take care.
-Koz
