<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>formmail php coding</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="contactformstyle.css" type="text/css">
</head>
<body>

<div id="header">your header can go here</div>

<!-- beginning of form -->
<div id="form">
<form method="post" action="contact_form.php">

<?php
/* initialize the variables */

$goesto="null";

// for user form submission - character lengths limited and spaces stripped
$name = substr(trim($_POST['name']), 0, 60);
$email = substr(trim($_POST['email']), 0, 60);
$comment = substr(trim($_POST['comment']), 0, 300);

// for processing - change nonstandard characters into character entities
$fixname = htmlentities($name);
$fixcomment = htmlentities($comment, ENT_QUOTES);

/* *** change yourdomainname to the domainname of the recipient
eg: $domain="crosswinds.net"; *** */

$domain="YOUR_ISP.com";

/* *** change 'recipientname' to that of the recipient
eg: $goesto = "j_smith"."@".$domain ; *** */

$goesto = "YOUR_NAME"."@".$domain ;
$subject = "comments from feedback form";

// see www.php.net/date for time setups
$today = date('\a\t H:i O \o\n l, j F, Y' ,time());

// for split email
$userName = "null";
$mailDomain = "null";
$sender_email = "null";

// set up how things will look on the email message
$message = "Name: $name\nemail: $email\nYour comments: $comment\n";
$extra = "From: $name\r\nReply-To: $email\r\n";

// begin validation
if (($name == "") || ($email == "") || ($comment == ""))
{
echo '<div id="oops">An error has occurred and the form cannot be sent. Please note that all areas of the form <em>must</em> be filled in. Either use your browser buttons to go back to the form or make corrections below.</div>'. "\n\n";
}

if ($name == "") {
echo '<p class="error"><label for="name">Your name</label> (required):<br /> <input type="text" id="name" name="name" /></p>';
}
else {
echo '<p>Your name:<br />';
echo '<input type="hidden" value="' , $fixname , '" id="name" name="name" />'; echo '<span class="done">' , $fixname , '</span></p>';
}

// require email with proper syntax
if ($email == "") {
echo '<p class="error"><label for="email">Your e-mail address</label> (required):<br /> <input type="text" id="email" name="email" /></p>';
}
elseif ((!ereg(".+\@.+\..+", $email)) || (!ereg("^[a-zA-Z0-9_@.-]+$", $email)))
{
echo '<p class="error">Please retype <label for="email">your e-mail address</label>:<br />'. "\n" .' <input type="text" name="email" id="email" /></p>'. "\n";
$email = ""; // - thanks Joaquim!!
}
else {
//split email address in half
list($userName, $mailDomain) = split('@', $email);
$sender_email = $userName."@".$mailDomain;
echo '<p>Your e-mail address:<br /> <input type="hidden" value="' , $sender_email , '" name="email" id="email" /><span class="done">' , $sender_email , '</span></p>';
}

// require comment
if ($comment == "") {
echo '<p class="error"><label for="comment">Your Brief Message</label> (required):<br /> <textarea name="comment" id="comment"></textarea></p>';
}
else {
echo '<p>Your Brief Message:<br /> <input type="hidden" id="comment" value="' ,$fixcomment, '" name="comment" /><span class="done">' , $fixcomment , '</span></p>';
}

// end validation

// send back the unfinished form if the required fields are not filled
if (($name == "") || ($email == "") || ($comment == ""))
{
echo '<p><input type="submit" value="Send Form" class="button" title="Before sending, please proofread. Go back ("alt + ?") to make correction(s)" /></p>';
}

// send the completed form if everything is filled out
else {
mail ($goesto, $subject, $message, $extra);

// set up confirmation message for webpage
echo '<div id="thnks"><p>Thank you for your message. We appreciate hearing from you, ' .$name. '. A copy of the message sent ' .$today. ' appears on this page. (Please note that this is only a test and this test message went nowhere.)</p></div>';
}

echo '</form></div>';

/* thanks mainly to Dan Ball
(www.webreference.com/programming/php/phpemail/2.html) for the terrific tutorial and www.php.net for the huge reference section; also multiple thanks to the crosswinds cadre (www.crosswinds-cadre.net), especially playfulkitten and ragbert for help in getting the thing to validate. And finally to Joaquim (www.nerd-boy.net) for the helping to get the validations to work completely */ ?>
<!-- end of form -->

<div id="footer">your footer can go here</div>

</body></html>

return to tutorial and/or