
Part 2 of our HTML Web Forms guide will be focused on creating the PHP file that will send the data entered in on our form to an email address. When it comes to processing forms, there are many different things you can do: you can send the information to a database, post it onto a page, and send it to an email address. The reason I chose to have my form handled by a PHP file that sends the data in an email, is because it was the easiest method I could find. If you search on Google you’ll find all kinds of ways to process a form, however just like in the last part of this guide, the biggest problem I had was integrating everything together with what I already had. This was largely due to the fact that I have minimal PHP knowledge, so not a lot of it made sense to me. Needless to say, because of that most of the methods I use in this part of the guide are pretty simple. So even if you don’t know anything about PHP either you should have no problems following this part of the guide. In fact while I was writing this part of the guide I came across a new way of handling check boxes, which actually made it a lot easier…and that’s good news for you!
If you haven’t already, I recommend reading through Part 1 of this guide, since you’ll need to edit the file you made in Part 1 at the end of this post. If you’ve already done Part 1, you should have a file named tutorial_form.html. Keep this file handy, since you’ll be editing a short line of code in it in step 10.
This is a multiple part series:
Keep checking back over the next few days for the next part!
STEP
1
The first thing you should do is make a new folder and put the file from the last part (tutorial_form.html) inside of it. You can name the folder whatever you’d like, but it will help to have that file and the one we’re creating in this part in the same spot. Now you need to open up a fresh file in your favorite text editor, or program of choice, make sure it is 100% blank and save it as tutorial_form_submit.php. This will be the file we’ll work in for this part. Remember to save your file after each step, so if your computer crashes or you make a mistake you don’t lose too much!
STEP
2
Now that you have your blank PHP file, you need to add in what is essentially the starting PHP tag.
<?php
For those of you that may be really new to PHP, you can think of this as the opening tag for all your PHP. Only unlike HTML, we’ll have a ton more things that go inside. Then at the end of the file we’ll close this tag, just like with an HTML tag
STEP
3
First will add in the lines that tell the PHP file to get the data from the text field, drop down list, and text area, and then give them a name the PHP file can recognize. The reason we’re doing these first is because they’re a little easier than the check boxes and radio buttons, and they all work the same way.
<?php $email = $_POST['email'] ; $age = $_POST['age'] ; $moredetails = $_POST['moredetails'] ;
The first line we added will be to handle the email text field in our form.
The next line is to handle the drop down list for the age ranges in our form.
The last line in this step is for our text area field, everything works the same as the other two above.
STEP
4
The next set of lines will be to get the data from the radio buttons and the check boxes. These work a little differently than the fields we worked with above, mostly because there’s multiple inputs that are all part of the same group. We need to take into account the “group” factor. Remember how we added brackets after the name of our check boxes and radio buttons? This is why!
<?php
$email = $_POST['email'] ;
$age = $_POST['age'] ;
$moredetails = $_POST['moredetails'] ;
foreach($_POST['color'] as $value) {
$color_msg .= "$value" ;
}
foreach($_POST['pets'] as $value) {
$pets_msg .= "$value, " ;
}
The first set of lines is for our radio buttons.
The next set of lines is for our check boxes, and works exactly like the radio button code did.
STEP
5
We’re going to start working on the code that handles the email. There’s quite a few options available, so I’m just going to cover what I see as the essentials. The first part of setting up the email is to tell the PHP file what email address to send it to, and what to use as the subject for the email.
<?php
$email = $_POST['email'] ;
$age = $_POST['age'] ;
$moredetails = $_POST['moredetails'] ;
foreach($_POST['color'] as $value) {
$color_msg .= "$value" ;
}
foreach($_POST['pets'] as $value) {
$pets_msg .= "$value, " ;
}
$to = $_POST['email'] ;
$subject = "Form Submission From Website.com" ;
STEP
6
Now we’re going to add in additional information for the email itself. This includes stuff like sending a copy to yourself (your own email address), as well as what email address to use if the user replies to the email they receive, and what email address to use if returned mail notification needs to be sent.
<?php
$email = $_POST['email'] ;
$age = $_POST['age'] ;
$moredetails = $_POST['moredetails'] ;
foreach($_POST['color'] as $value) {
$color_msg .= "$value" ;
}
foreach($_POST['pets'] as $value) {
$pets_msg .= "$value, " ;
}
$to = $_POST['email'] ;
$subject = "Form Submission From Website.com" ;
$headers .= "From: Your Name Here <youremail@website.com>\r\n" ;
$headers .= "Cc: youremail@website.com\r\n" ;
$headers .= "Return-Path: youremail@website.com\r\n" ;
$headers .= "Reply-To: youremail@website.com\r\n" ;
STEP
7
Now we’re going to make the body of the email, this is where all of the data we collected will go. You can make this area as fancy as you want with HTML, but I prefer to stick with plain text, since you really just want a simple summary of the information.
<?php
$email = $_POST['email'] ;
$age = $_POST['age'] ;
$moredetails = $_POST['moredetails'] ;
foreach($_POST['color'] as $value) {
$color_msg .= "$value" ;
}
foreach($_POST['pets'] as $value) {
$pets_msg .= "$value, " ;
}
$to = $_POST['email'] ;
$subject = "Form Submission From Website.com" ;
$headers .= "From: Your Name Here <youremail@website.com>\r\n" ;
$headers .= "Cc: youremail@website.com\r\n" ;
$headers .= "Return-Path: youremail@website.com\r\n" ;
$headers .= "Reply-To: youremail@website.com\r\n" ;
$body = "Thank you for sending in your data! Below is a summary of what we've received:
Email: $email
Favorite Color: $color_msg
Pets: $pets_msg
More Details:
$moredetails" ;
STEP
8
There’s only a few more lines to go, but they’re very important! The first one we’re going to add is telling the PHP file to send the email, using the names we setup in steps 5, 6, and 7. This is very important, since without it the email would never leave the file, or it wouldn’t contain the correct information.
<?php
$email = $_POST['email'] ;
$age = $_POST['age'] ;
$moredetails = $_POST['moredetails'] ;
foreach($_POST['color'] as $value) {
$color_msg .= "$value" ;
}
foreach($_POST['pets'] as $value) {
$pets_msg .= "$value, " ;
}
$to = $_POST['email'] ;
$subject = "Form Submission From Website.com" ;
$headers .= "From: Your Name Here <youremail@website.com>\r\n" ;
$headers .= "Cc: youremail@website.com\r\n" ;
$headers .= "Return-Path: youremail@website.com\r\n" ;
$headers .= "Reply-To: youremail@website.com\r\n" ;
$body = "Thank you for sending in your data! Below is a summary of what we've received:
Email: $email
Favorite Color: $color_msg
Pets: $pets_msg
More Details:
$moredetails" ;
if (mail($to,$subject,$body,$headers)) {
This line is telling the PHP file that “if” the data is present and the file has run correctly (and later on in this series, if that data passes verification) to send an email using the following data, and then it lists off our $to, $subject, $body, and $header names, to send the email using that data. Please note that the order the names are listed inside of mail() is very important, and you shouldn’t have them listed in any other order, or your file may not work correctly! Also take note of the parenthesis and the curly bracket, make sure you include them in the correct order.
STEP
9
Now we’re going to tell the file to redirect the user to a specific page once the email has been sent. Generally you’ll want to create some sort of confirmation page, so the user knows the data has been sent. I discourage against just redirecting them back to the form, since that may lead to them submitting the form multiple times without knowing it was actually sent. They may even just give up and go elsewhere if they think they’re not getting through. We’re also going to close out our PHP file and then we’ll be done!
<?php
$email = $_POST['email'] ;
$age = $_POST['age'] ;
$moredetails = $_POST['moredetails'] ;
foreach($_POST['color'] as $value) {
$color_msg .= "$value" ;
}
foreach($_POST['pets'] as $value) {
$pets_msg .= "$value, " ;
}
$to = $_POST['email'] ;
$subject = "Form Submission From Website.com" ;
$headers .= "From: Your Name Here <youremail@website.com>\r\n" ;
$headers .= "Cc: youremail@website.com\r\n" ;
$headers .= "Return-Path: youremail@website.com\r\n" ;
$headers .= "Reply-To: youremail@website.com\r\n" ;
$body = "Thank you for sending in your data! Below is a summary of what we've received:
Email: $email
Favorite Color: $color_msg
Pets: $pets_msg
More Details:
$moredetails" ;
if (mail($to,$subject,$body,$headers)) {
header ('Location: http://www.dvorakdesigns.com') ;
}
exit ();
?>
Remember to save this file as tutorial_form_submit.php, and put it in the folder that you created in step 1, along with the HTML file we’ve already created.
STEP
10
Okay, finally the last step! For this you need to open up your HTML file, tutorial_form.html. We need to tell our form to run the PHP file we just made when someone hits the submit button.
<form name="myform" method="post" action="tutorial_form_submit.php">
Just type in the name of your PHP file between the quotation marks for action. If you post a form on your site, I recommend putting in the full URL to the file, just to be safe, though for this tutorial putting in the name will work fine if both files are in the same folder.
That’s all for this part! If you’re able to, upload your form and the PHP file to a web server to test it out. The form won’t function right off your computer because it needs to utilize PHP, so if you don’t have a web server to use you’ll just have to trust that you did all the steps right until you can get to one! In the next part we’ll work on adding in some verification to the different fields, to make sure we collect all the data we want to. Adding in verification can sometimes help ward off spam robots too, it’s not a guarantee to stop all spam, but it can definitely help (for sure if you have a long form). If you have any questions about this part, feel free to post a comment below and I’ll answer it as soon as I can!
Just want to say what a great blog you got here!
I’ve been around for quite a lot of time, but finally decided to show my appreciation of your work!
Thumbs up, and keep it going!
Cheers
Christian, iwspo.net