December 10th, 2008

Submit a Form The Simple Way

This is one of those posts that could get hairy real fast.

I’ll do my best to keep it simple, brief, and to the point.

Let’s say you want to gather some info from your visitors.

You don’t want to connect to an autoresponder. You don’t want to deal with cgi, let alone code in it.

You just want some data forwarded from your web page to you via email.

Well, you’ve come to the right place.

Here’s some rapid enlightment for you.

First, you’ve got 3 screenshots, followed by the code that drives them, and finally a link to the zip file that does it all.

Screenshot: Submit Form Entry

Submit Form Entry Sample

Once the “Send” button is clicked the following appears.

Screenshot: Submit Form Send

Submit Form Send Sample

A moment later, the email is received and looks like this.

Screenshot: eMail Received

eMail Received Example

Now here is the code for the first screen shot.

You’ll reference it later in the zip file as contactus_entry.php.

<html>
<title>Contact Us – Entry</title>
<body>

<form method=”post” action=”contactus_send.php”>

Name:
<br>
<input type=”text” name=”contact_name” size=30>
<br><br>

eMail:
<br>
<input type=”text” name=”contact_email” size=30>
<br><br>

Message:
<br>
<textarea name=”contact_message” cols=40 rows=5 wrap=virtual></textarea>
<br><br>

Sex:
<br><select name=”contact_sex”>
<option value =”void”>not yes or no</option>
<option value =”Male”>Male</option>
<option value =”Female”>Female</option>
</select>
<br><br><br>

<input type=”submit” name=”submit” value=”Send”>
<br><br>

</form>

</body>
</html>

Here is the code for the second screen shot.

You’ll reference it later in the zip file as contactus_send.php.

<?

$msg = “Contact Us Entry\n”;
$msg .= “Name: $contact_name\n”;
$msg .= “eMail: $contact_email\n”;
$msg .= “Message: $contact_message\n”;
$msg .= “Sex: $contact_sex\n”;

$to = “YOUREMAIL@YOURSITE”;
$subject=”Contact Us Submission”;

$mailheaders = “From: The Contact Us Site <> \n”;
$mailheaders .= “Reply-To: $contact_email\n\n”;

mail($to, $subject, $msg, $mailheaders);

?>
<html>
<title>Contact Us – Send</title>
<body>

<h2>The following email has been sent</h2>

Name:
<br>
<? echo “$contact_name”; ?>
<br><br>

eMail:
<br>
<? echo “$contact_email”; ?>
<br><br>

Message:
<br>
<? echo “$contact_message”; ?>
<br><br>

Sex:
<? echo “$contact_sex”; ?>

</body>
</html>

All of the images and code can be downloaded here.

If you enjoy and find value in this post, I would like to invite you to check out my most recent website: Dream Town Market. If you find it’s a site you’d like to visit again please be sure to share it with others.

Oh, and one more thing. I want to show you the code above actually works.

Here’s the link. And yes, it’s “LIVE”. I will get your messages. And no, you don’t have to specify your sex. 😉

Which leads to another idea. Not sex. The fact that there is no error checking. If you’d like to see the expansion of the code to include this, “stay tuned”. I’ll be making a post on that topic within the next week in the Code Lounge.

Thanks for reading!

Patrick Scott Donovan aka xearther

P.S. Thank you, Ollie.

by xearther