Using PHP to Email Web Form Data

If you want to solve the big problem of emailing a web page’s form contents, you must use a backend solution such as PHP, Coldfusion, JSP, etc.

Quite often, web designers can use programs such as Dreamweaver to create the HTML form elements but not the code that will read and send the name/value pairs from a form.

I chose to give you PHP code that will parse the specific form that I am giving you. Be sure that if you make any changes to the HTML code that you update the PHP code as well.

HTML Form Code

This form uses fieldset tags, legend tags, and CSS display styling to create a form layout that does not need a table to look good.

<h1>Contact Us</h1>
<form id="submitform" action="submitform.php" method="post">
	<fieldset>
		<legend>Your Information</legend>
		<label for="fname">First Name:</label>
<input id="fname" name="fname" type="text" />
		<label for="lname">Last Name:</label>
<input id="lname" name="lname" type="text" />
		<label for="email">Your Email:</label>
<input id="email" name="email" type="text" />
		<label for="dayphone">Day Phone Number:</label>
<input id="dayphone" name="dayphone" type="text" />
		<label for="nightphone">Night Phone Number:</label>
<input id="nightphone" name="nightphone" type="text" />
	</fieldset>
	<fieldset>
		<legend>Comment or Question</legend>
		<textarea id="comment" name="comment"></textarea>
	</fieldset>
<div class="centertxt">
<input name="submitbtn" type="submit" value="Send it!" /></div>
</form>

This code should give you a form like the following:

Web Form

CSS Code to Style the Form

Copy this CSS and place it in an internal or an external style sheet (CSS).

label {
        display: block;
}
input[type="text"], textarea {
	font-family: Arial, Sans-Serif;
	font-size: .8em;
	margin-left: 12px;
	margin-bottom: 4px;
	display: block;
	padding: 4px;
	border: solid 1px #bbb;
	width: 300px;
	background-color: #fff;
}
.centertxt {
	text-align: center;
	margin: 12px;
	padding: 6px;
}

PHP code to parse the form

You need to create a new web page and name it “submitform.php” If you look in the form tag in the above HTML, you will see the “action” attribute that sends form name/value pairs to the action page.

The new submitform.php page should include the following code in the head of the page:

	$fullname = $_POST['fname']." ".$_POST['lname'];
	$emailaddress = $_POST['email'];
	$daytimephone = $_POST['dayphone'];
	$nighttimephone = $_POST['nightphone'];
	$fullcomment = $_POST['comment'];
 
	$msg = "A new comment has been submitted.\n" .
	"Name: $fullname\n" .
	"Day time phone number: $daytimephone\n" .
	"Night time phone number: $nighttimephone\n" .
	"Your comment: $fullcomment";
 
	$subject = "Comment from web site";
	$email = "$emailaddress";
	// Change the value of the $to variable to match the email address
        // that should receive the comment emails.
        $to = "youremail@email.com";
 
	mail($to, $subject, $msg, 'From:'.$email);

You should receive an email sent to the email address found in the $to variable in the above PHP code.

Email submission

Next, the following code should go in the body section of the parsing page. This is the code that will thank your user for submitting a comment through the form.

	echo "<strong>$fullname, thank you for submitting your comment.</strong>";
	echo "You will soon receive more information from us.";

Your visitor should then receive a confirmation page that looks like:

Submission results

Download the files

You can download the necessary files. Unzip and place in your web site’s directory. Change your email address in the ‘processform.php’ file. Make sure you upload the files to a PHP server. This code needs a PHP server to work.

This entry was posted in Web Design. Bookmark the permalink. Follow any comments here with the RSS feed for this post. Post a comment or leave a trackback: Trackback URL.

One Comment

  1. Gary
    Posted February 11, 2010 at 2:15 pm | Permalink

    Thanks for the help. Just wondering though…
    Are the bots not able to sniff the “youremail@email.com” out of the “submitform.php”?

Post a Comment

Your email is never published nor shared. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">