Order Form Part 10: The PHP Script - Confirmations

By Vince Barnes

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:

//we do not need the rest of the form fields as we can just calculate them from the whole numbers
if( !empty($_POST["qtyA"]) )
    $qtyA = $_POST["qtyA"];
    $totalA = $qtyA * 1.25;
    $qtyA = 0;
    $totalA = 0;
if( !empty($_POST["qtyB"]) )
    $qtyB = $_POST["qtyB"];
    $totalB = $qtyB * 2.35;
    $qtyB = 0;
    $totalB = 0;
if( !empty($_POST["qtyC"]) )
    $qtyC = $_POST["qtyC"];
    $totalC = $qtyC * 3.45;
    $qtyC = 0;
    $totalC = 0;
$GrandTotal = $totalA + $totalB + $totalC;

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:

//we have our data, and now build up an email message to send
$mailto = "orders@ourdomain.com";
$subject = "Web Order";
$body  = "The following confirms the details of your order:\n";
$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($mailto, $subject, $body);
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:

//we should state the order was sent
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

Make a Comment

Loading Comments...

  • Web Development Newsletter Signup

    Invalid email
    You have successfuly registered to our newsletter.
Thanks for your registration, follow us on our social networks to keep up-to-date