<html>
<head>
<title>Process HTML form + send thru email</title>
</head>
<body bgcolor=lightyellow>
<h1><center>The Ice Cream Shop</center></h1>
<h2>Collect data from HTML. eMail the content</h2>
<?php
error_reporting(E_ALL ^ E_WARNING ^ E_NOTICE); //all error but warnigns & notices
$to = 'sam.sultan@nyu.edu'; #multiple recepients separated with ','
$subject = 'Web Server Mail'; #subject of the mail - cannot have \n
$body = ''; #multi-lines - lines cannot exceed 70 char
// $cc = 'someone@abc.com, someone@xyz.com'; #(optional)
// $bcc = 'someone@nyu.edu'; #(optional)
// $replyTo = 'someone@nyu.edu'; #(optional)
$format = 'html'; #(optional) - plain or html
if ($_POST) #if data was collected in _POST array
{ #that means it was the 2nd time around
validate();
if ($msg == '') #if no errors
{
mail_data();
}
}
display();
//=============================================================================
function validate()
{
global $firstname, $lastname, $address, $flavor, $topping, $creditCard;
global $name_error,$addr_error,$flav_error,$top_error,$cc_error,$msg;
$firstname = $_POST['firstname']; #get HTML form entry fields
$lastname = $_POST['lastname'];
$address = $_POST['address'];
$flavor = $_POST['flavor']; #select list --> array
$topping = $_POST['topping']; #checkboxes --> array
$creditCard = $_POST['creditCard'];
$msg;
$name_error;
$addr_error;
$flav_error;
$top_error;
$cc_error;
if (is_array($flavor))
$flavor = implode(',' , $flavor); #flatten out the array
if (is_array($topping))
$topping = implode(',' , $topping); #flatten out the array
if ($firstname == '' || $lastname == '') {
$msg = 'error';
$name_error = '*';
}
if ($address == '') {
$msg = 'error';
$addr_error = '*';
}
if ($flavor == '') {
$msg = 'error';
$flav_error = '*';
}
if ($topping == '') {
$msg = 'error';
$top_error = '*';
}
if ($creditCard[0] == '') {
$msg = 'error';
$cc_error = '*';
}
if ($msg == 'error')
$msg = 'Please enter required field(s) above!';
}
//===============================================================================
function mail_data()
{
global $to, $subject, $body, $cc, $bcc, $replyTo, $format, $msg;
$headers = "Cc: " . $cc . "\n";
$headers .= "Bcc: " . $bcc . "\n";
$headers .= "Reply-To: " . $replyTo . "\n";
$headers .= "Content-Type: text/" . $format . "; charset=utf-8 \n";
$body = "<h3>Your mail is... </h3>";
$body .= "<table border=1>";
foreach($_POST as $fieldName => $fieldValue)
{
if (is_array($fieldValue))
$fieldValue = implode(',' , $fieldValue);
$body .= "<tr><td><b>$fieldName: <td> $fieldValue";
}
$body .= "</table>";
$ok = mail($to, $subject, $body, $headers); //mail the data
$msg = ($ok)
? "Mail sent successfully"
: "Mail was not suceessful";
}
//===============================================================================
function display()
{
global $firstname, $lastname, $address, $flavor, $topping, $creditCard;
global $name_error,$addr_error,$flav_error,$top_error,$cc_error,$msg;
if ($creditCard == 'visa') $visa_checked = 'CHECKED';
if ($creditCard == 'master-card') $mc_checked = 'CHECKED';
if ($creditCard == 'amex') $amex_checked = 'CHECKED';
if (substr_count($flavor,'vanilla') > 0) $vanl_selected = 'SELECTED';
if (substr_count($flavor,'chocolate') > 0) $choc_selected = 'SELECTED';
if (substr_count($flavor,'strawberry') > 0) $strw_selected = 'SELECTED';
if (substr_count($flavor,'butter-pecan') > 0) $butr_selected = 'SELECTED';
if (substr_count($flavor,'rocky-road') > 0) $rock_selected = 'SELECTED';
if (substr_count($flavor,'french-vanilla') > 0) $fren_selected = 'SELECTED';
if (substr_count($flavor,'pistachio') > 0) $pist_selected = 'SELECTED';
if (substr_count($topping,'hotFudge') > 0) $hotF_checked = 'CHECKED';
if (substr_count($topping,'sprinkles') > 0) $sprk_checked = 'CHECKED';
if (substr_count($topping,'nuts') > 0) $nuts_checked = 'CHECKED';
if (substr_count($topping,'whippedCream') > 0) $whip_checked = 'CHECKED';
print "<form method=POST action=/~sultans/php/demo/6file/formToMail.php> \n";
print "<fieldset style='width:570px;border-color:gold'> \n";
print "<legend>Enter Fields Below</legend> \n";
print "<table bgcolor=eeeeee> \n";
print "<tr>";
print "<td><b>Enter First Name ";
print "<td><input type=text name=firstname value='$firstname'> \n";
print "<tr>";
print "<td><b>Last Name <font color=red> $name_error </font>";
print "<td><input type=text name=lastname value='$lastname'> \n";
print "<tr>";
print "<td><b>Enter Address <font color=red> $addr_error </font>";
print "<td><textarea name=address rows=4 cols=47>$address</textarea> \n";
print "<tr>";
print "<td><b>Ice Cream Flavor <font color=red> $flav_error </font> \n";
print "<td><select name='flavor[]' SIZE='4' multiple='multiple'> \n";
print " <option value='vanilla' $vanl_selected> Vanilla</option> \n";
print " <option value='chocolate' $choc_selected> Chocolate</option> \n";
print " <option value='strawberry' $strw_selected> Strawberry</option> \n";
print " <option value='butter-pecan' $butr_selected> Butter Pecan</option> \n";
print " <option value='rocky-road' $rock_selected> Rocky Road</option> \n";
print " <option value='french-vanilla' $fren_selected> French Vanilla</option> \n";
print " <option value='pistachio' $pist_selected> Pistachio</option> \n";
print "</select> \n";
print "<tr>";
print "<td><b>Select Topping <font color=red> $top_error </font> \n";
print "<td>";
print "<input type='checkbox' name='topping[]' value='hotFudge' $hotF_checked/> Hot Fudge \n";
print "<input type='checkbox' name='topping[]' value='sprinkles' $sprk_checked/> Sprinkles \n";
print "<input type='checkbox' name='topping[]' value='nuts' $nuts_checked/> Nuts \n";
print "<input type='checkbox' name='topping[]' value='whippedCream' $whip_checked/> Whipped Cream \n";
print "<tr>";
print "<td><b>Choose Credit Card <font color=red> $cc_error </font> \n";
print "<td>";
print "<input type=radio name=creditCard value='visa' $visa_checked/> Visa \n";
print "<input type=radio name=creditCard value='master-card' $mc_checked/> Master Card \n";
print "<input type=radio name=creditCard value='amex' $amex_checked/> Amex \n";
print "<tr>";
print "<td width=155>";
print "<input type=submit value=' Place Order '>";
print "<td><input type=reset value=Cancel> \n";
print "</table> \n";
print "</fieldset> \n";
print "<br><font color=red> $msg </font>";
print "<br/>";
print "</form> \n";
}
//===============================================================================
?>
<?php include "../include.php"; ?> <!-- hyperlink to see the code -->
</body>
</html>