Credit Card Display Forms in PHP/MySQL--Part 2 of Chapter 7 from Usable Shopping Carts (4/8)

To page 1To page 2To page 3current pageTo page 5To page 6To page 7To page 8
[previous] [next]

Usable Shopping Carts, Chapter 7: CC Validation and Verification

Now we can notify the user that the order's been processed (at least so far as the onsite database in concerned).

<h2>Thank You!</h2><p>You order in the amount of $<?php printf("%.2f",$order_total); ?> has been processed.</p>
<?php

While we're at it, let's also send the customer a confirming e-mail.

Setting up e-mail support in PHP is usually quite easy, if it's not already configured on your server. It's a little different on Windows and Unix, but in both cases it involves setting a variable in the php.ini file. For Win32 platforms, the SMTP configuration variable is set to the name or IP address of your mailserver, e.g. SMTP=mail.tunein.site. In addition, the sendmail_from configuration directive can be set to a desired return address, for example, sendmail_from=shopping_cart@tunein.site. If you're running a test server in your home or office, you can even set this to the same mail server you use for your outgoing personal e-mail. (Don't do this for sending more than a few test e-mails without checking with your ISP or system administrator! You could run afoul of someone's anti-spamming policy.) For Unix-style servers, we set the sendmail_path configuration variable to the correct path on the system to sendmail or its equivalent; in most cases this will be sendmail_path=usr/sbin/sendmail or sendmail_path=usr/lib/sendmail; if you're using Qmail, this path is likely to be var/qmail/bin/sendmail or var/qmail/bin/qmail-inject. If you encounter problems, check your system's documentation or ask the administrator for the system, network, ISP or site host that you're using.

We get the customer's e-mail address and name from the customers table and assign their values to the variable $to and $name; and we assign a suitable subject line string to the variable $subject as well.

    $c_query = "SELECT customer_first_name,customer_last_name,
 customer_email ";
    $c_query .= "FROM customers WHERE session_id=$s_id";
    $c_result = mysql_query($c_query);
    $c_row = mysql_fetch_assoc($c_result);
    $to = $c_row["customer_email"];
    $name = $c_row["customer_first_name"] . " " . 
                                             $c_row["customer_last_name"];
    $subject = "TuneIn! Order Confirmation";

We'll store the body of the e-mail in a variable named $message, which we'll build up as we go along, beginning with an appropriate greeting:

    $message = "Dear $name,\n\nYour order for\n\n";

What follows is essentially the same code that we used to generate the Cart Detail display for the page cart.php. The only substantial difference is that we're generating plain text rather than HTML, so we do our formatting with ASCII linebreak and tab characters (\n and \t).

    $a_query = "SELECT c.cart_id,c.quantity,pg.product_group_title,
                   f.format_description,pc.product_code_price ";
    $a_query .= "* CASE WHEN ISNULL(s.special_percentage) OR 
                     s.start_date>CURDATE() OR s.end_date<CURDATE() ";
    $a_query .= "THEN 1 ELSE (100-s.special_percentage)*.01 END 
                     AS price FROM product_codes pc ";
    $a_query .= "LEFT JOIN product_groups pg USING (product_group_id) ";
    $a_query .= "LEFT JOIN formats f ON f.format_id=pc.format_id ";
    $a_query .= "LEFT JOIN carts c 
                     ON pc.product_code_id=c.product_code_or_event_id ";
    $a_query .= "LEFT JOIN specials s 
                     ON pc.product_code_id=s.product_group_or_event_id ";
    $a_query .= "WHERE c.session_id='$s_id' AND c.item_type_id=1";
    $a_result = mysql_query($a_query);
    $album_total = 0;
    if(mysql_num_rows($a_result) > 0)
    {
      while($a_row = mysql_fetch_assoc($a_result))
      {
        $c_id = $a_row["cart_id"];
        $quantity = $a_row["quantity"];
        $price = $a_row["price"];
        $code = $a_row["product_or_event_code"];
        $title = $a_row["product_group_title"];
        $format = $a_row["format_description"];
        $item_amount = $quantity * $price;
        $album_total += $item_amount;
        $message .= "($quantity)\t$format -- $title @ \$";
        $message .= sprintf("%.2f", $price) . "--> \t\$" 
                                     . sprintf("%.2f", $item_amount);

The sprintf() function works to format output in a similar fashion to printf(), and takes the same sorts of arguments. The only difference is that sprintf() returns a string instead of outputting it immediately.

        $message .= "\n";
      }
    }
    $message .= "\n\n";
    $ticket_total = 0;
    //  ticket detail -- similar to that used for cart.php...
    $t_query = "SELECT c.cart_id,c.quantity,e.event_name,
                                    e.event_datetime,e.event_price ";
    $t_query .= "* CASE WHEN ISNULL(s.special_percentage) 
                 OR s.start_date>CURDATE() OR s.end_date<CURDATE() ";
    $t_query .= "THEN 1 ELSE (100-s.special_percentage)*.01 END 
                               AS price,v.venue_city,st.state_code ";
    $t_query .= "FROM events e LEFT JOIN venues v USING (venue_id) ";
    $t_query .= "LEFT JOIN states st USING (state_id) ";
    $t_query .= "LEFT JOIN tunein.carts c 
                             ON e.event_id=c.product_code_or_event_id ";
    $t_query .= "LEFT JOIN tunein.specials s ON 
                               e.event_id=s.product_group_or_event_id ";
    $t_query .= "WHERE c.session_id='$s_id' AND c.item_type_id=2";
    $t_result = mysql_query($t_query);

To page 1To page 2To page 3current pageTo page 5To page 6To page 7To page 8
[previous] [next]

Created: December 26, 2002
Revised: December 26, 2002

URL: http://webreference.com/programming/carts/chap7/2/4.html