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 = "email@example.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”); ?>
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 . . .
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 “. When you’ve received e-mail you’ve probably noticed that the sender’s name shows up in the From field in your inbox. This is because of the way the header was set up. When the user sends a message from your page, the name they provided will show up in your inbox.
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.
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 comments and questions are welcome.