Email Forms In PHP, The Easiest Yet...
By Dan Ball
2003-09-27

Processing The Mail
Take a look at the code below. It is very typical of any scripting language, server-side or client-side, so if you have any amount of knowledge in JavaScript, ASP, JSP, Perl or the like, you are going to look at this and it will be very familiar.
The code is a combination of validation and if validated, the email processor as well. Take a look at the first if statement, it is checking to see if any of the fields are empty, if any one field is, it then opens a form and prints a statement to say they did miss some required field(s).
The statements after that are also simple if statements that check each individual fields to see if they are empty, if they are empty, it prints that field again to be filled in (note that the form was already opened above if a field was empty as well), if it is not empty it then stores the value of that field in a hidden field.
<?php
if (($Name == "") || ($Email == "") || ($Comments == ""))
{
echo "<form name=form method=post action=contact_thanks.php>";
echo "<p class=bodymd>All three fields of this form are required,
I really don't think that's too much to ask...</p>";
echo "<p class=bodymd>Fill in the ones you missed, they are listed
below.</p>";
}
if ($Name == "")
{
echo "<p class=bodymd>Your Name<br><input type=text name=Name></p>";
}
else
{
echo "<input type=hidden name=Name value=$Name>";
}
if ($Email == "")
{
echo "<p class=bodymd>Your Email<br><input type=text name=Email></p>";
}
else
{
echo "<input type=hidden name=Email value=$Email>";
}
if ($Comments == "")
{
echo "<p class=bodymd>Comments or Questions<br><textarea
name=Comments rows=5 cols=40></textarea></p>";
}
else
{
echo "<input type=hidden name=Comments value=$Comments>";
}
if (($Name == "") || ($Email == "") || ($Comments == ""))
{
echo "<input type=submit name=Submit value=Submit>";
echo "<input type=reset name=Reset value=Clear Form>";
echo "</form>";
}
else
{
$message = "Name: $Name\nEmail: $Email\nComments: $Comments\n";
$extra = "From: $Name\r\nReply-To: $Email\r\n";
mail ("noone@nowhere.com", "Website Email", $message, $extra);
echo "<p class=bodymd>Thanks for your inguiry, $Name.</p>";
echo "<p class=bodymd>A response will be sent to $Email as soon as
possible.</p>";
}
?>
The final if statement above is again checking for empty fields. If any of the required fields are empty, it will print submit and clear buttons as well as the closing form tag to complete the form that has been opened earlier to re-submit the form. Once re-submitted it will be re-validated by the same process. Once all of the required fields are filled in, it then hits the else option of the last if. This is where the mail is processed and sent.
If you found this article interesting, you may want to read these as well:
» Protecting your PHP and HTML Source Code
» Publishing Newsletters Using PHP & MySQL - 4
» Publishing Newsletters Using PHP & MySQL - 3
» Publishing Newsletter Using PHP & MySQL - 2
» Publishing Newsletters Using PHP & MySQL
» Unix Webserver Crontab Basics
|