📄 ch23.htm
字号:
Cart</TH></TR><BR>
<BR>
EOT<BR>
}<BR>
<BR>
# Print the selected product list for the current page.<BR>
<BR>
sub printbody {<BR>
local (@itemlist) = @_;<BR>
<BR>
foreach(@itemlist) {<BR>
($name, $price) = split (/@/,$iteminfo{$_});<BR>
print <<EOT;<BR>
<TR><TD ALIGN=right> $input{$_, qty}</TD><BR>
<TD><INPUT TYPE="text" size=3 NAME="$_
qty" VALUE=1></TD><BR>
<TD>$name</TD><BR>
<TD ALIGN=right><B>\$$price</B></TD><BR>
<TD ALIGN=center><INPUT TYPE=checkbox NAME="$_ add"</TD>
<BR>
</TR><BR>
EOT<BR>
}<BR>
}<BR>
<BR>
# Print the HTML footer for the product pages<BR>
sub printfooter {<BR>
<BR>
local ($gobutton) = @_;<BR>
print <<EOT;<BR>
</TABLE><BR>
<P><BR>
<CENTER><BR>
<INPUT TYPE=submit VALUE="$gobutton" NAME="button">
<BR>
<INPUT TYPE=submit VALUE="View Cart" NAME="button">
<BR>
<INPUT TYPE=reset><BR>
</CENTER><BR>
</FORM><BR>
<BR>
</BODY><BR>
</HTML><BR>
EOT<BR>
}<BR>
<BR>
# Write current state by using hidden form input. Prepend "old"
to<BR>
# The variable names to make them easy to recognize.<BR>
<BR>
sub makehidden {<BR>
@custinfo = ('customer','address','city','state',<BR>
'zipcode','email','payment_method', 'cardno');
<BR>
<BR>
foreach (@custinfo) {<BR>
print "<INPUT TYPE=hidden NAME=$_ VALUE=\"$input{$_,$empty}\">"
<BR>
if $input{$_,$empty} ne $empty;<BR>
}<BR>
<BR>
<BR>
}<BR>
<BR>
# Print the HTML header for the Cart Contents page<BR>
<BR>
sub cartheader {<BR>
<BR>
print <<EOT;<BR>
<HTML><HEAD><BR>
<TITLE>CGI Unleashed Accessory Depot</TITLE><BR>
</HEAD><BR>
<BR>
<BODY bgcolor="#FFFFFF"><BR>
<CENTER><BR>
<H2>Cart Contents</H2><BR>
</CENTER><BR>
<BR>
<FORM METHOD=post ACTION=cart.cgi><BR>
<TABLE width=100% border><BR>
EOT<BR>
}<BR>
<BR>
# Print the HTML footer for the Cart Contents page<BR>
<BR>
sub cartfooter {<BR>
&makehidden;<BR>
<BR>
print <<EOT;<BR>
</table><BR>
<BR>
<p><BR>
<center><BR>
<INPUT TYPE=submit name="button" VALUE="Page
One"><BR>
<INPUT TYPE=submit name="button" VALUE="Page
Two"><BR>
<INPUT TYPE=submit name="button" VALUE="Delete
Selected Items"><p><BR>
<BR>
<table border><BR>
<tr><td colspan=2 align=center><BR>
<font size=+1>Order Info: Complete Before Placing Order</font></td>
<BR>
<tr><td><BR>
<b>Name:</b> <INPUT TYPE=text SIZE=20 name="customer"
<BR>
VALUE="$input{customer,$empty}"></td><BR>
<td><b>Address:</b> <INPUT TYPE=text SIZE=20
name="address"<BR>
VALUE="$input{address,$empty}"></td></tr>
<BR>
<td><b>Email:</b> <INPUT TYPE=text SIZE=20
name="email"<BR>
VALUE="$input{email,$empty}"></td><BR>
<td><b>City:</b> <INPUT TYPE=text SIZE=20
name="city"<BR>
VALUE="$input{city,$empty}"><br></td></tr>
<BR>
<tr><td><b>State/Prov.:</b> <INPUT
TYPE=text SIZE=15 name="state"<BR>
VALUE="$input{state,$empty}"></td><BR>
<td><b>Zip/Postal Code:</b> <INPUT TYPE=text
SIZE=7 name="zipcode"<BR>
VALUE="$input{zipcode,$empty}"></td></tr>
<tr><td colspan=2 align=center><BR>
<b>Payment Method:</b><BR>
Check: <INPUT TYPE=radio name="payment_method" VALUE="check">
<BR>
"Unleashed" Card: <INPUT TYPE=radio name="payment_method"
VALUE="credit"><BR>
Money Order: <INPUT TYPE=radio name="payment_method"
VALUE="moneyorder"><BR>
</td></tr><BR>
<tr><td align=center colspan=2><BR>
<b>"Unleashed" Card # (if applicable)</b>
<BR>
<INPUT TYPE=text NAME="cardno" size=8<BR>
VALUE="$input{cardno,$empty}"></td></tr>
<tr><td colspan=2 align=center><BR>
<INPUT TYPE=submit NAME="button" VALUE="Place
this Order"><BR>
<INPUT TYPE=reset> </td></tr><BR>
</table><BR>
</center><BR>
EOT<BR>
<BR>
print "</form>";<BR>
}<BR>
<BR>
# Mail the order information to the person indicted in $mailto
<BR>
<BR>
sub mailorder {<BR>
<BR>
local (@orderlist) = @_;<BR>
@required=('customer','address','city','zipcode','state',<BR>
'email','payment_method');<BR>
<BR>
if ($input{payment_method,$empty} eq "credit") {<BR>
&printerror if (length($input{cardno,$empty})
!= 7);<BR>
}<BR>
<BR>
foreach (@required) {<BR>
&printerror unless defined $input{$_,$empty};
<BR>
}<BR>
<BR>
if ($#orderlist > -1) {<BR>
open (MAIL, "|/usr/sbin/sendmail -t");<BR>
print MAIL<<EOM;<BR>
To: $mailto<BR>
From: Shopping Cart CGI<BR>
Subject: New Order<BR>
<BR>
Customer Info:<BR>
$input{customer,$empty}<BR>
$input{address,$empty}<BR>
$input{city,$empty}, $input{state,$empty}<BR>
$input{zipcode,$empty}<BR>
$input{email,$empty}<BR>
<BR>
Paying by: $input{payment_method,$empty}<BR>
Number: $input{cardno,$empty}<BR>
EOM<BR>
<BR>
foreach (@orderlist){<BR>
($name, $price) = split (/@/,$iteminfo{$_});<BR>
$input {$_, qty} = int ($input{$_, qty});<BR>
print MAIL "$input{$_, qty} $name @\$$price";
<BR>
$itemtotal = $price * $input{$_, qty};<BR>
$subtotal = $subtotal + $itemtotal;<BR>
}<BR>
$tax = $subtotal * $taxrate;<BR>
$grandtotal = $subtotal + $tax;<BR>
printf MAIL "Subtotal: \$%4.2f\n", $subtotal;
<BR>
printf MAIL "Tax: \$%4.2f\n", $tax;<BR>
printf MAIL "Total: \$%4.2f\n", $grandtotal;
<BR>
<BR>
&printthanks<BR>
}<BR>
else {<BR>
&printerror<BR>
}<BR>
close (MAIL);<BR>
exit 3;<BR>
}<BR>
<BR>
# Print a Thank You message once the order has been sent<BR>
sub printthanks {<BR>
print <<EOT;<BR>
<HEAD><HTML><BR>
<TITLE>Thank You!</TITLE><BR>
</HEAD><BR>
<BR>
<BODY bgcolor="#FFFFFF"><BR>
<BR>
<center><BR>
<h2>Thank you for your order, $input{$customer, $empty}</h2>
<BR>
Please wait 6-8 weeks for delivery.<BR>
</center><BR>
</BODY><BR>
</HTML><BR>
EOT<BR>
<BR>
exit 4;<BR>
}<BR>
<BR>
# Print an error message if an error is encountered when mailing
the order.<BR>
sub printerror {<BR>
<BR>
print <<EOT;<BR>
<HEAD><HTML><BR>
<TITLE>Ooops!</TITLE><BR>
</HEAD><BR>
<BR>
<BODY bgcolor="#FFFFFF"><BR>
<center><BR>
<h2>The Shopping Cart CGI has Encountered an Error.</h2>
<BR>
Either your cart was empty or you did not include all<br>
<BR>
necessary information in the order form.<br><BR>
Please go back and try again.<BR>
<BR>
<p><BR>
<FORM ACTION=cart.cgi METHOD=post><BR>
<INPUT TYPE=submit name="button" VALUE="Go Back
and try Again."><BR>
EOT<BR>
<BR>
&makehidden;<BR>
<BR>
print <<EOT;<BR>
</center><BR>
</FORM><BR>
</BODY><BR>
</HTML><BR>
EOT<BR>
<BR>
exit 5;<BR>
}</FONT></TT>
</BLOCKQUOTE>
<HR>
<P>
The following is an example of the HTML output generated by <TT><FONT FACE="Courier">cart.cgi.
T</FONT></TT>his is a product listings page. Notice from the hidden
input fields that before this page was generated, the customer
had added 100 T-Shirts and 60 keychains to her shopping cart.
Obviously a bulk buyer!<BR>
<HR>
<BLOCKQUOTE>
<B>Listing 23.5. The HTML output produced by the shopping cart
code.<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier"><HTML><HEAD><BR>
<TITLE>CGI Unleashed Accessory Depot</TITLE><BR>
</HEAD><BR>
<BR>
<BODY bgcolor="#FFFFFF"><BR>
<CENTER><BR>
<H2>CGI Programming Unleashed<br>Accessory Depot</H2>
<BR>
</CENTER><BR>
<BR>
<FORM METHOD=post ACTION=cart.cgi><BR>
<TABLE width=100% border><BR>
<TR><TH>Qty.</TH><TH>Item</TH><TH>Price</TH><TH>Add</TH></TR>
<BR>
<BR>
<INPUT TYPE=hidden NAME="oldT add"<BR>
VALUE="on"><BR>
<INPUT TYPE=hidden NAME="oldT qty"<BR>
VALUE="100"><BR>
<INPUT TYPE=hidden NAME="oldkeychain add"<BR>
VALUE="on"><BR>
<INPUT TYPE=hidden NAME="oldkeychain qty"<BR>
VALUE="60"><BR>
<TR><TD><INPUT TYPE="text" size=3 NAME="bball
qty" VALUE=1></TD><BR>
<TD>"Team CGI" Baseball Jerseys</TD><BR>
<TD ALIGN=right><B>$39.95</B></TD><BR>
<TD ALIGN=center><INPUT TYPE=checkbox NAME="bball
add"</TD><BR>
</TR><BR>
<TR><TD><INPUT TYPE="text" size=3 NAME="plate
qty" VALUE=1></TD><BR>
<TD>"Unleashed" Commemorative Plates</TD>
<BR>
<TD ALIGN=right><B>$29.95</B></TD><BR>
<TD ALIGN=center><INPUT TYPE=checkbox NAME="plate
add"</TD><BR>
</TR><BR>
<TR><TD><INPUT TYPE="text" size=3 NAME="coaster
qty" VALUE=1></TD><BR>
<TD>"Unleashed" Coasters, set of 6</TD>
<BR>
<TD ALIGN=right><B>$4.95</B></TD><BR>
<TD ALIGN=center><INPUT TYPE=checkbox NAME="coaster
add"</TD><BR>
</TR><BR>
<TR><TD><INPUT TYPE="text" size=3 NAME="nitelite
qty" VALUE=1></TD><BR>
<TD>"Unleashed" Glow-in-the-Dark Nite-Lite</TD>
<BR>
<TD ALIGN=right><B>$2.95</B></TD><BR>
<TD ALIGN=center><INPUT TYPE=checkbox NAME="nitelite
add"</TD><BR>
</TR><BR>
</TABLE><BR>
<P><BR>
<CENTER><BR>
<INPUT TYPE=submit VALUE="Previous Page" NAME="button">
<BR>
<INPUT TYPE=submit VALUE="View Cart" NAME="button">
<BR>
<INPUT TYPE=reset><BR>
</CENTER><BR>
</FORM><BR>
<BR>
</BODY><BR>
</HTML></FONT></TT>
</BLOCKQUOTE>
<HR>
<H2><FONT SIZE=5 COLOR=#FF0000><A NAME="Summary">Summary</FONT></H2>
<P>
This chapter outlined a simple but adequate shopping cart for
most applications. Without too much effort, you should be able
to customize the program and the catalog presented in this chapter
to suit your own needs. Using the tools explained in this chapter
its possible to build a state-of-the-art shopping cart CGI for
any application.
<P>
In this chapter you learned how to do the following:
<UL>
<LI><FONT COLOR=#000000>Develop the core shopping cart features</FONT>
<LI><FONT COLOR=#000000>Use Hidden Input Types as a simple way
of maintaining state</FONT>
<LI><FONT COLOR=#000000>Use </FONT><TT><FONT FACE="Courier">.htaccess</FONT></TT>
and <TT><FONT FACE="Courier">REMOTE_USER</FONT></TT> to maintain
state
<LI><FONT COLOR=#000000>Develop HTTP cookies for persistent client-side
state</FONT>
<LI><FONT COLOR=#000000>Access and manipulate DBM file</FONT>s
</UL>
<P>
<HR WIDTH="100%"></P>
<CENTER><P><A HREF="ch22.htm"><IMG SRC="pc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A><A HREF="#CONTENTS"><IMG SRC="cc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A><A HREF="index.htm"><IMG SRC="hb.gif" BORDER=0 HEIGHT=88 WIDTH=140></A><A HREF="ch24.htm"><IMG
SRC="nc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A></P></CENTER>
<P>
<HR WIDTH="100%"></P>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -