The field named $key contained $formdata{$key}"; print $toHTML; $order .= "$key = $formdata{$key}\n"; if (( $key eq "email" ) || ( $key eq "EMAIL" )) { $address = $formdata{$key}; $address .= "\n"; } } $order .= "======================================"; print "
Final Order: $order"; print "
Final Address: $address"; ## Open and append to customer record file. open (CUSTORDERS, ">>/home/fmrfreek/public_html/CGI/LOGS/syngsdsorders.txt"); ## flock file to protect against lost update and ## other damage from simultaneous use. flock (CUSTORDERS, 2); print CUSTORDERS "$order"; close (CUSTORDERS); flock (CUSTORDERS, 8); ## Release hold on file. if ($address ne "none") { ## Send email confirmation containing order info. open (MAIL, "|/usr/sbin/sendmail -t"); print MAIL "To: $address From: $from"; print MAIL "Subject: $subjct"; print MAIL "$order"; close (MAIL); } print "
Thank you for your order.
"; ================================================= Final Project source code, continued: server-side form data parsing subroutine: ================================================= sub Parse_Form { ## This subroutine parses form input. ## It also displays the parsed form data. ## It displays versatility and has security ## provisions. ## This parsing script was sourced from the ## book, PERL and CGI for the World Wide Web: Visual ## QuickStart Guide (Peachpit Press : Berkeley, CA), ## 1999. web reference: http://www.cookwood.com ## It has been modified in several significant ## ways by Christopher M. Balz. ## All commenting is by Christopher M. Balz. ## The first task is to load the @pairs array. ## How it loaded depends on which method (METHOD) ## was used to upload it from the browser to this ## server script. We use the environment variable ## REQUEST_METHOD to see what the method used is. ## Environment variables are stored in a hash named ## $ENV{'AN_ENVIRONMENTVARIABLE'}. if ($ENV{'REQUEST_METHOD'} eq 'GET') { @pairs = split(/&/, $ENV{'QUERY_STRING'}); } elsif ($ENV{'REQUEST_METHOD'} eq 'POST') { ## With the POST method, we must first read the ## data from the standard input. To do this, we ## must specify the length of the data in bytes. ## This number is stored in the environment ## variable CONTENT_LENGTH. $buffer is just a ## temporary holding spot variable. read (STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); ## Next, we can split the data as before with ## the GET method: @pairs = split(/&/, $buffer); ## Check the QUERY_STRING environment variable ## to see if any data has been appended to the ## URL in addition to the data sent with the ## POST method: if ($ENV{'QUERY_STRING'}) { @getpairs = split (/&/, $ENV{'QUERY_STRING'}); ## Add the @getpairs array to the end of @pairs. push(@pairs, @getpairs); } } ## At this point, we know that neither ## GET nor POST has been used. else { ## Direct the output to the browser: print "Content-type: text/html\n\n"; ## Write the HTML to the browser: print "Error: Must use POST or GET."; } ## This section processes the data in the @pairs array. ## The data from @pairs is loaded into a hash named ## $formdata{$key}. Thus, the $formdata hash is a ## hash of name-value pairs. foreach $pair (@pairs) ## Steps through the @pairs array, ## element by element. { ## The form's NAME=VALUE pair goes into $key and $value, ## respectively. ($key, $value) = split (/=/, $pair); $key =~ tr/+/ /; ## Replace + signs with spaces. ## Here we convert the hexadecimal representation of $key into ## ASCII, to spell the NAME value. $key =~ s/%(..)/pack("c", hex($1))/eg; $value =~ tr/+/ /; ## Replace + signs with spaces. ## Here we convert the hexadecimal representation of $value into ## ASCII, to spell the VALUE value. $value =~ s/%(..)/pack("c", hex($1))/eg; ## This eliminates any possible server side includes (SSI) from ## this (incoming) data, so that nothing unknown is executed ## (as a security precaution). $value =~ s///g; ## This if conditional checks to see if the NAME (stored ## in the variable $formdata{$key}) has been already been ## assigned to the hash - perhaps because it has multiple ## values, like a menu or a set of checkboxes. if ($formdata{$key}) { ## If so, add the VALUE to any existing VALUES that ## correspond to the NAME in question. $formdata {$key} .= ", $value"; } else { $formdata{$key} = $value; } } ## End of foreach loop. ## End of parsing sequence. } 1; ## Must return true to be called successfully from the main script. ===================================================== Here is the purchasing and order formulation section of the ecommerce site: (final project), followed by ere is the customer data form section of the ecommerce site: (final project) ===================================================== ======================================================= ======================================================= //Shopping cart library functions //Suzanne Stagel //Foothill College-Summer 1999 //COIN 70-JavaScript //----------------- //Global Variables //----------------- var today = new Date(); var exp = new Date(); //Define cookie expiration exp.setTime(today.getTime() + 1000*60*60*24*365); //--------- //getCookie //--------- // //Parameters: name of the cookie to retrieve //Functionality: returns the value of the cookie with the specified name // function getCookie(Name) { var search = Name + "="; if (document.cookie.length > 0) { //if there are any cookies offset = document.cookie.indexOf(search) if (offset != -1) { // if cookie exists offset += search.length; //set index of beginning of value end = document.cookie.indexOf(";", offset); // set index of end of cookie value if (end == -1) end = document.cookie.length; return unescape(document.cookie.substring(offset, end)); } } } //------------------ //initializeCookies //------------------ // //Parameters: None //Functionality: initializes cookie value for each product to 0 and sets the cookies // on the user's machine // //Note: A maximum of 20 distinct products can be accomodated in the shopping cart // because this implementation is limited by the number of cookies that a // browser will allow a domain to set (this includes items added and later deleted // from the cart) // // Alert statements that are commented out are for debugging purposes only. // function initializeCookies() { //Variables var cookieValue = -1; var defaultQty = 0; //Define cookie expiration var today = new Date(); var exp = new Date(); exp.setTime(today.getTime() + 1000*60*60*24*365); // //Set initial cookies for Tomatoes // cookieValue = getCookie("brandywine"); if ( cookieValue != null ) { // alert("Cookie brandywine exists, original value: " + cookieValue); } else { setCookie("brandywine",defaultQty,exp); // alert("New cookie brandywine added with value of value: " + getCookie("brandywine")); } cookieValue = getCookie("earlyGirl"); if ( cookieValue != null ) { // alert("Cookie earlyGirl exists, original value: " + cookieValue); } else { setCookie("earlyGirl",defaultQty,exp); // alert("New cookie earlyGirl added with value of value: " + getCookie("earlyGirl")); } cookieValue = getCookie("cherokeePurple"); if ( cookieValue != null ) { // alert("Cookie cherokeePurple exists, original value: " + cookieValue); } else { setCookie("cherokeePurple",defaultQty,exp); // alert("New cookie cherokeePurple added with value of value: " + getCookie("cherokeePurple")); } cookieValue = getCookie("chadwickCherry"); if ( cookieValue != null ) { // alert("Cookie chadwickCherry exists, original value: " + cookieValue); } else { setCookie("chadwickCherry",defaultQty,exp); // alert("New cookie chadwickCherry added with value of value: " + getCookie("chadwickCherry")); } // //Set initial cookies for Watermelons // cookieValue = getCookie("yellowMoon"); if ( cookieValue != null ) { // alert("Cookie yellowMoon exists, original value: " + cookieValue); } else { setCookie("yellowMoon",defaultQty,exp); // alert("New cookie yellowMoon added with value of value: " + getCookie("yellowMoon")); } cookieValue = getCookie("moonbeam"); if ( cookieValue != null ) { // alert("Cookie moonbeam exists, original value: " + cookieValue); } else { setCookie("moonbeam",defaultQty,exp); // alert("New cookie moonbeam added with value of value: " + getCookie("moonbeam")); } // //Set initial cookies for Veggies // cookieValue = getCookie("carrot"); if ( cookieValue != null ) { // alert("Cookie carrot exists, original value: " + cookieValue); } else { setCookie("carrot",defaultQty,exp); // alert("New cookie carrot added with value of value: " + getCookie("carrot")); } cookieValue = getCookie("fourLettuce"); if ( cookieValue != null ) { // alert("Cookie fourLettuce exists, original value: " + cookieValue); } else { setCookie("fourLettuce",defaultQty,exp); // alert("New cookie fourLettuce added with value of value: " + getCookie("fourLettuce")); } cookieValue = getCookie("redLettuce"); if ( cookieValue != null ) { // alert("Cookie redLettuce exists, original value: " + cookieValue); } else { setCookie("redLettuce",defaultQty,exp); // alert("New cookie redLettuce added with value of value: " + getCookie("redLettuce")); } cookieValue = getCookie("peas"); if ( cookieValue != null ) { // alert("Cookie peas exists, original value: " + cookieValue); } else { setCookie("peas",defaultQty,exp); // alert("New cookie peas added with value of value: " + getCookie("peas")); } cookieValue = getCookie("peas"); if ( cookieValue != null ) { // alert("Cookie peas exists, original value: " + cookieValue); } else { setCookie("peas",defaultQty,exp); // alert("New cookie peas added with value of value: " + getCookie("peas")); } cookieValue = getCookie("corn"); if ( cookieValue != null ) { // alert("Cookie corn exists, original value: " + cookieValue); } else { setCookie("corn",defaultQty,exp); // alert("New cookie corn added with value of value: " + getCookie("corn")); } } //end initializeCookies //----------- //listCookies //----------- // //Parameters: None //Functionality: lists the values of all the cookies set by the site (in the form of dialog alert boxes) // //Note: This function is used for debugging purposes only. // function listCookies() { //Tomatoes alert("earlyGirl: " + getCookie("earlyGirl")); alert("brandywine: " + getCookie("brandywine")); alert("cherokeePurple: " + getCookie("cherokeePurple")); alert("chadwickCherry: " + getCookie("chadwickCherry")); //Watermelons alert("yellowMoon: " + getCookie("yellowMoon")); alert("moonbeam: " + getCookie("moonbeam")); //Veggies alert("carrot: " + getCookie("carrot")); alert("fourLettuce: " + getCookie("fourLettuce")); alert("redLettuce: " + getCookie("redLettuce")); alert("peas: " + getCookie("peas")); alert("corn: " + getCookie("corn")); alert("listCookies completed"); } //end listCookies //--------- //setCookie //--------- // //Parameters: name (of the cookie), value (of the cookie), expiration date of the cookie (optional) //Functionality: Sets a cookie on the user's machine. // function setCookie(name, value, expire) { document.cookie = name + "=" + escape(value) + ((expire == null) ? "" : ("; expires=" + expire.toGMTString())) } //setCookie //--------- //addToCart //--------- // //Parameters: item (name of the cookie) //Functionality: "adds" the item to the shopping cart by incrementing the value of the cookie // by 1 and calling the updateCart() function to dynamically update the cartFrame's HTML // //Note: Alert statements that are commented out are for debugging purposes only. // function addToCart(item) { var cookieName = item; var cookieValue = -1; var newQuantity = 0; //check for existing cookie for this item //if exists, overwrite (adding 1), otherwise create cookie with value of 1 cookieValue = getCookie(item); if ( cookieValue != null ) { // alert(item + "cookie exists, original value: " + cookieValue); newQuantity = Number(cookieValue) + 1; } else { newQuantity = 1; } setCookie(item,newQuantity,exp); //For debugging only // cookieValue = getCookie(item); // alert("New cookie value for " + item + " is: " + cookieValue); //update shopping cart display updateCart(); } //addToCart //---------- //deleteItem //---------- // //Parameters: item (name of the cookie) //Functionality: "deletes" the item from the shopping cart by setting the value of the cookie // to 0 and calling the updateCart() function to dynamically update the cartFrame's HTML // //Note: Alert statements that are commented out are for debugging purposes only. // function deleteItem(item) { var cookieName = item; var cookieValue = -1; var newQuantity = 0; cookieValue = getCookie(item); if ( cookieValue != null ) { //For debugging only // alert("Deleting existing item " + item + " with a quantity of " + cookieValue + " from cart"); } else { alert("Error in deleteItem(): Trying to delete an item not in the shopping cart"); } setCookie(item,newQuantity,exp); //update shopping cart display updateCart(); //For debugging only // alert(item + " value is now " + getCookie(item)); } //deleteItem //----------- //subtractOne //----------- // //Parameters: item (name of the cookie) //Functionality: subtracts one "item" from the shopping cart by decrementing the value of the cookie // by 1 and calling the updateCart() function to dynamically update the cartFrame's HTML // //Note: Alert statements that are commented out are for debugging purposes only. // function subtractOne(item) { var cookieName = item; var cookieValue = 0; var newQuantity = 0; //check for existing cookie for this item //if exists, overwrite (subtracting 1), otherwise display error alert cookieValue = getCookie(item); if ( cookieValue != null ) { if (cookieValue > 0) { // alert(item + "cookie exists, original value: " + cookieValue); //For debugging only newQuantity = Number(cookieValue) - 1; setCookie(item,newQuantity,exp); } else { alert("Error in subtractOne(): Trying to subtract from an item with a quantity <= 0"); } } else { alert("Error in subtractOne(): Trying to subtract from an item not in the shopping cart"); } //For debugging only // cookieValue = getCookie(item); // alert("New cookie value for " + item + " is: " + cookieValue); //update shopping cart display updateCart(); } //subtractOne //--------------- //updateQuantity //--------------- // //Parameters: item (name of the cookie), quantity (new quantity in shopping cart) //Functionality: updates the quantity of "item" in the shopping cart by setting // its value to "quantity" and calling the updateCart() function to dynamically update the cartFrame's HTML // //Note: Alert statements that are commented out are for debugging purposes only. // function updateQuantity(item,quantity) { var cookieName = item; var cookieValue = 0; //check for existing cookie for this item //if exists, overwrite (with quantity passed in), otherwise display error alert cookieValue = getCookie(item); if ( cookieValue != null ) { // alert(item + "cookie exists, original value: " + cookieValue); //For debugging only newQuantity = quantity; setCookie(item,quantity,exp); } else { alert("Error in updateQuantity(): Trying to update from an item not in the shopping cart"); } //For debugging only // cookieValue = getCookie(item); // alert("New cookie value for " + item + " is: " + cookieValue); //update shopping cart display updateCart(); } //updateQuantity //---------- //updateCart //---------- // //Parameters: None //Functionality: dynamically creates new HTML for cartFrame based on the cookie values set // for each product. // //Note: Alert statements that are commented out are for debugging purposes only. // function updateCart() { var cartHTML="