#!/usr/bin/perl # Remember to change the above line if it's different on # your system. ############################################################### # THIS SECTION OF THE SCRIPT CONTAINS VARIABLES THAT YOU # # *MUST* CUSTOMIZE FOR YOUR OWN SITE! # # # # This variable defines exactly where the orders will end up. # # It should be a complete path to a file OUTSIDE your server # # space. (See the text in Chapter 13 for details.) # $order_file = "COMPLETE-PATH-TO-ORDER-FILE"; # This is the name of the current script. You need to define # # it here so that the script can generate a new form that # # refers to $order_script. # $order_script = "order.cgi"; # This is the URL of your home page, or whichever page you # # want to return them to after they order: # $home_page = "http://YOUR-SITE"; # The $t variable defines what will be used to separate # # fields in your order file. We have set it to the tab # # character ("\t")by default; this should work well if you # # are going to be accessing the orders via a spreadsheet or # # database application. You could also use semicolons, # # pipes, asterisks, etc. DON'T use spaces. # $t = "\t"; # # ############################################################### # Convert the form input to a usable form, using the # 'ReadParse' subroutine from "cgi-lib.pl": require "cgi-lib.pl"; &ReadParse; # First, you need to check to see that they entered all the # required data. If they didn't, send them to the "Missing" # subroutine, which will prompt them again. # The first 'if' statement (actually a 'do-if' statement, # because the action that might be taken comes *before* the # logical expressions) has a number of expressions of the # form '!$x'; those say, "if $x does NOT exist." These # expressions are separated by || markers; this means that # "Missing" will be executed if ANY of those do not exist. # (The || is a logical OR operator.) &Missing if ( !$in{'NAME'} || !$in{'ADDRESS'} || !$in{'CITY'} || !$in{'STATE'} || !$in{'ZIP'} || !$in{'PHONE'} || !$in{'ACCOUNT'} || !$in{'EXPDATE'} ); # The second 'if' statement has && markers between the # expressions; this means that "Missing" will be executed # only if ALL "Quantity" fields do not exist. (The && is a # logical AND operator.) &Missing if ( !$in{'QUANTITY1'} && !$in{'QUANTITY2'} && !$in{'QUANTITY3'} ); # Print the HTTP header and a new page that # shows people what they entered. print "Content-type: text/html\n\n"; print< Confirmation of Your Order

Here's what you ordered:

Name: $in{'NAME'}
Address: $in{'ADDRESS'}, $in{'CITY'}, $in{'STATE'}  $in{'ZIP'}
Phone: $in{'PHONE'}
Ship via: $in{'DELIVERY'}
VISA Card #: $in{'ACCOUNT'}
Exp. Date #: $in{'EXPDATE'}

Items:
ENDOFTEXT # If they only ordered one item, there's no need to print out # blank entries. You can use three 'if' statements that are # only executed if their respective entries exist. if ( $in{'QUANTITY1'} ) { print "#$in{'CODE1'}, $in{'ITEM1'} "; print "(Quantity: $in{'QUANTITY1'})
"; } if ( $in{'QUANTITY2'} ) { print "#$in{'CODE2'}, $in{'ITEM2'} "; print "(Quantity: $in{'QUANTITY2'})
"; } if ( $in{'QUANTITY3'} ) { print "#$in{'CODE3'}, $in{'ITEM3'} "; print "(Quantity: $in{'QUANTITY3'})
"; } # Finish up the confirmation page by giving a link back to # your home page: print< Back to our home page ENDOFTEXT # Open a filehandle called ORDERS, and have the contents of # ORDERS *appended* to the end of the file specified with the # $order_file variable. This is done with a >> symbol before # the file name (a *single* > would completely *replace* the # order file). open ( ORDERS, ">>$order_file" ); # Dump all the order information into the order file, represented # by the ORDERS filehandle. Every piece of information will be # separated by the field separator, which is defined by $t. # At the end of the data, a new line character ("\n") is sent. # (You can reorganize these if you like; you could also use the # "CurrentTime" subroutine from the Guest Book script and # ainclude its output as a field.) Note that as we have written # it here, it it simply one 'print' statement with lots of # arguments. print ORDERS "$in{'NAME'}$t", "$in{'PHONE'}$t", "$in{'ADDRESS'}$t", "$in{'CITY'}$t", "$in{'STATE'}$t", "$in{'ZIP'}$t", "$in{'ACCOUNT'}$t", "$in{'EXPDATE'}$t", "$in{'DELIVERY'}$t", "$in{'CODE1'}$t", "$in{'ITEM1'}$t", "$in{'QUANTITY1'}$t", "$in{'CODE2'}$t", "$in{'ITEM2'}$t", "$in{'QUANTITY2'}$t", "$in{'CODE3'}$t", "$in{'ITEM3'}$t", "$in{'QUANTITY3'}$t", "\n"; # Close the ORDERS filehandle. close (ORDERS); ############################################################## # The "Missing" subroutine takes care of situations where the # some of the required fields are left blank. sub Missing { # Print the HTTP header and a new form that tells people to # supply whichever information was missing. This output will # essentially be the same as the original order form, but # with values inserted where they DID provide information the # first time around, by way of 'value' attributes in the # tags. print "Content-type: text/html\n\n"; print< Need more information Some information is missing from this form. You must fill in all of the fields in the top part of the form, and you must specify at least one item to order.

Please add the missing information and resubmit the form.


Name:
Street address:
City: State: Zip:
Phone:
VISA Card Number: Exp. Date:

Delivery Method:
U.S. Mail
Federal Express (by 10:30 AM tomorrow)

Item 1
Product No.: Title: Quantity:
Item 2
Product No.: Title: Quantity:
Item 3
Product No.: Title: Quantity:


You may this order, or all of the fields and start over.
ENDOFTEXT # The 'exit' statement makes the script stop right here in the # subroutine, rather than returning to the main program. The # curly bracket officially finishes off the subroutine. exit; }