Click to go home
Code for add.pl (called when 'Add to Cart' is clicked)
#!perl
# $RCSfile: add.pl,v $
# $Revision: 1.1 $
# add item to cart
use CGI;
# CGI::Carp tries to trap errors and display in browser
use CGI::Carp "fatalsToBrowser";
use CartUtilities;
$q = new CGI;
$pid = $q->param("pid"); # retrieve product id number
if ($pid && $pid =~ /^\d{3,4}$/)
{
add_item_to_cart("products",$ENV{"HTTP_COOKIE"},$pid);
print("Content-Type: text/html\n\n");
print($q->start_html());
print(<< BLOCK);
Your selection has been added.
< table>
< tr>
< td>
< form action="/cgi-bin/view.pl">
< input type="submit" value="VIEW CART">
< /form>
< /td>
< td>
< form action="/cgi-bin/register.pl">
< input type="submit" value="CHECKOUT">
< /form>
< /td>
< /tr>
< /table>
BLOCK
}
else
{
print($q->header(), $q->start_html());
print("Invalid request");
}
print($q->end_html());
exit();
Code for update.pl
#!perl
# $RCSfile: update.pl,v $
# $Revision: 1.1 $
use CGI;
use CGI::Carp "fatalsToBrowser";
use DBI;
use Utilities;
use CartUtilities;
$q = new CGI;
$pid_str = "";
@form_names = $q->param();
$dbh = "";
$sth = "";
$user = "";
$auth = "";
$dsn = "demo";
$driver = "ODBC";
$total = 0;
$sql = "";
%products = update_cart("products",$ENV{"HTTP_COOKIE"},@form_names);
print "Content-Type: text/html\n\n";
@pids_in_cart = keys(%products);
print $q->start_html();
if (@pids_in_cart)
{
# quote strings for SQL
@pids_in_cart = quote_strings(@pids_in_cart);
$pid_str = join(" OR pid=", @pids_in_cart);
$sql = "SELECT * FROM products WHERE pid=$pid_str";
print << HTML;
< style type="text/css">
div { color:green; margin-left:10%; margin-right:5%; margin-top:5%; }
< /style>
< div>< form action="/cgi-bin/update.pl">
< table>
< tr valign="top">< th>Title< /th>
< th>Qty< /th>< th>Check to remove item< /th>< /tr>
HTML
$dbh = DBI->connect("dbi:$driver:$dsn",$user,$auth) || die(DBI->errstr);
$sth = $dbh->prepare($sql) || die($dbh->errstr);
$sth->execute() || die($sth->errstr);
while (my @row = $sth->fetchrow())
{
$row[5] = sprintf("%.2f", $row[5]);
print qq(< tr>< td>$row[1] $row[5]< /td>);
print qq(< td align="center">< input size=5 name="$row[0]");
print qq(value="$products{$row[0]}">< /td>);
print qq(< td align="center">< input type="checkbox" name="del");
print qq( value="$row[0]"> DEL< /td>< /tr>\n);
$total += $row[5] * $products{$row[0]};
}
$total = sprintf("%.2f",$total);
print(qq(< tr>< td>< b>TOTAL: < u>\$$total< /u>< /b>< /td>));
print(qq(< td>< input type="submit" value="MODIFY ORDER">< /td>));
print("< /tr>< /table>< /form>< /div>");
$sth->finish();
$dbh->disconnect();
}
else
{
print("Nothing in cart\n");
}
print($q->end_html());
exit();
Next Page - View Cart
Jump to:
Book Search |
Checkout
Go to top