Order Form Part 10: The PHP Script - Confirmations
Now that our script has verified that we have good data, we need to round out our compliment of data elements. It would be possible to retrieve all these elements from the form, relying on the JavaScript calculations for the amounts. That would be a reasonable idea, since it would mean that the calculations are only performed in one place and would be easier to maintain, but, for the sake of illustration once again, we are going to calculate the amount fields in our server-side PHP script.
Here's the code we need:
if( !empty($_POST["qtyA"]) )
{
$qtyA = $_POST["qtyA"];
$totalA = $qtyA * 1.25;
}
else
{
$qtyA = 0;
$totalA = 0;
}
{
$qtyB = $_POST["qtyB"];
$totalB = $qtyB * 2.35;
}
else
{
$qtyB = 0;
$totalB = 0;
}
{
$qtyC = $_POST["qtyC"];
$totalC = $qtyC * 3.45;
}
else
{
$qtyC = 0;
$totalC = 0;
}
Nothing too complicated about that!
OK! Now that we have all our data elements we can send out our confirmation email, and another to our own "Order Processing Department" to let them (our self, perhaps?!!) know that an order has been entered. One thing to remember when sending out these confirmation emails, though it doesn't apply to our present example, is that it is unwise (or worse) to include any credit card information in an email. It's always worth paying attention to the kind of information you do send via email -- sensitive information is just that, sensitive.
So, to the emails:
$mailto = "orders@ourdomain.com";
$subject = "Web Order";
$body .= "\n\n";
$body .= "Name: " . $Name . "\n";
$body .= "Email: " . $Email . "\n";
$body .= "Other Contact Info: " . $OtherInfo . "\n";
$body .= "\n\n";
$body .= "Class A Widgets: (" . $qtyA . " * 1.25) = " . $totalA . "\n";
$body .= "Class B Widgets: (" . $qtyB . " * 2.35) = " . $totalB . "\n";
$body .= "Class C Widgets: (" . $qtyC . " * 3.45) = " . $totalC . "\n";
$body .= "\n";
$body .= "TOTALS: " . $GrandTotal . "\n";
mail($Email, $subject, $body);
Note that when building the body of the email, the first line uses the
assignment operator = while the remaining use the concatenate assignment
operator .= I thought it worth a mention because it's easy to miss
the period.
If you wanted to have a different lead line for your order department than for the customer's confirmation email, you could build a second body and send that one to your order desk. PHP makes life very easy, all things considered! There are, of course, lots of other ways you could accomplish this, but, we only really need one, don't we?!!
We can also use the body of our email to return a confirmation page to the browser of our site visitor / customer, like this:
echo "The following information was sent.";
echo "<br>\n";
echo "<pre>\n";
echo $body;
echo "</pre>\n";
PHP is so elegant, don't you think? We're almost done with processing our form
Return to the previous step | Proceed to the next step