#!/usr/bin/perl -w #use CGI.pm Pretty to output xhtml tags use CGI::Pretty qw(:standard); #use CGI.pm Carp to output error messages #use CGI::Carp "fatalsToBrowser"; #NEW Student Assignment Submission Form #======================================= #I/we declare that the attached assignment is my/our own work in accordance with #Seneca Academic Policy. No part of this assignment has been copied manually or #electronically from any other source (including web sites) or distributed to other #students. #Name(s) Student ID(s) #----------------------------------------------------------------------------------- # MELEDY ANG 016-518-029 i3222d12 #print the http header print header(); #print header(); # Current page ID $currentpage=param("currentpage"); # Requested page ID $goto=param("goto"); # Product description and quantity if ($currentpage >= 1) { # Get product name and price $product=param("product"); # Get product price from $product and adjust to 2 decimals ($unitprice=$product) =~s/^.*\$//; $unitprice=sprintf("%.2f",$unitprice); # Get the product description from $product ($productdesc=$product) =~s/\$.*//; # Get the quantity $qty=param("qty"); # Do calculations $extprice=sprintf("%.2f",$qty*$unitprice); $gst=sprintf("%.2f",0.07*$extprice); $pst=sprintf("%.2f",0.08*$extprice); $ttl=sprintf("%.2f",$extprice+$gst+$pst); } # Set up a hash of province keys %provpc=qw/NF A NS B PE C NB E QC GHJK ON KLMNP MB R SK S AB T BC V NT X YK Y/; # Personal Information if ($currentpage >= 3) { $name=param("name"); $email=param("email"); $phone=param("phone"); $address1=param("address1"); $address2=param("address2"); $city=param("city"); $province=param("province"); $postcode=param("postcode"); # Name - 2-40 letters, spaces, dashes or periods $valid{name}=($name=~/^[-a-zA-Z. ]{2,40}$/); # E-mail - "user@host.domain" or "name " # or "user@host.domain (name)" $valid{email}=($email=~/^[[:alpha:]][-.\w]{0,19}\@(\w{2,}\.)+\w{2,}$| ^[^<>()]*<[[:alpha:]][-.\w]{0,19}\@(\w{2,}\.)+\w{2,}>$| ^[[:alpha:]][-.\w]{0,19}\@(\w{2,}\.)+\w{2,}\s*\([^<>()]*\)$/x); # Phone number - Collect 10 digits, reformat as "(999) 999-9999" $valid{phone}=($phone=~s/^\D*([2-9]) \D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d) \D*(\d)\D*(\d)\D*(\d)\D*$ /\($1$2$3\) $4$5$6-$7$8$9$10/x); # Address line 1 - 3-40 printable characters $valid{address1}=($address1=~/^[[:print:]]{3,40}$/); # Address line 2 - 0-40 printable characters $valid{address2}=($address2=~/^[[:print:]]{0,40}$/); # City - 2-40 printable characters starting with a capital letter $valid{city}=($city=~/^[A-Z][[:print:]]{1,39}$/); # Postal code - reformat as "A9A 9A9" $valid{postcode}=($postcode=~s/([a-z]\d[a-z])\s*(\d[a-z]\d)$/uc("$1 $2")/ie); # Province must be valid since it's a popup_menu $valid{province}=1; # Determine whether the postal code is valid for the province $valid{postcode}=($valid{postcode} && (index($provpc{$province}, substr($postcode,0,1))+1)); # We use 'grep' to search the values of %valid to see # if there are any invalid fields. $all_valid is set to true # if none of the fields are invalid. $page3_valid=!(grep(! $_ ,values(%valid))); # The values that were changed ($phone and $postcode) # need to be placed back into the CGI param list so that # if we use these fields to draw a web form, the # modified values will be shown. # # If we don't do this then these fields will be redrawn # using their old values. # param("phone",$phone); param("postcode",$postcode); } ## Determine the page to print ## #if ($goto eq "history" && !$valid{email} ) { # # history1(); # #elsif ($goto eq "history" ) { # # history2(); # #} els if (!$currentpage || $goto eq "1") { page1(); } elsif ($currentpage eq "1" ) { page2(); } elsif ($currentpage eq "2" || ! $page3_valid) { page3(); } elsif ($currentpage eq "3") { page4(); } # ---------------- SHOW IMAGE FUNCTION ------------------------------------ sub showImage { $productdesc=$product; if ($productdesc=~m/IBM ThinkPad T23/) { print img({-src=>'http://cache.techdepot.com/comassets/productlarge/2188958Lg.jpg', -width=>'250', -height=>'200', -alt=>'$notebook_name{$brand}', -align=>'middle'}); } elsif ($productdesc=~m/SONY VAIO PCG-GRS 150/) { print img({-src=>'http://cache.techdepot.com/comassets/productlarge/2362765Lg.jpg', -width=>'250', -height=>'200', -alt=>'$notebook_name{$brand}', -align=>'middle'}); } elsif ($productdesc=~m/HP Pavilion ze1230/) { print img({-src=>'http://cache.techdepot.com/comassets/productlarge/2391749Lg.jpg', -width=>'250', -height=>'200', -alt=>'$notebook_name{$brand}', -align=>'middle'}); } else { print img({-src=>'http://cache.techdepot.com/comassets/productlarge/2191095Lg.jpg', -width=>'250', -height=>'200', -alt=>'$notebook_name{$brand}', -align=>'middle'}); } } # ----------------- Page 1 - Catalog page ---------------------------- sub page1 { print start_html(-title=>"Page 1",-bgcolor=>'#CCCC99', -text=>'#808000', -vlink=>' #808000', -alink=>'#808000'), h1("Welcome to our DIGITAL company"), start_form({-method=>"get"}), hidden({-name=>"currentpage",-value=>"1"}); print "
\n"; &showImage; print "
\n", table({-border=>0}, Tr(td("Please select one of our remarcable products:"), td(popup_menu{-name=>"product", -values=>["IBM ThinkPad T23 \$1399.00", "SONY VAIO PCG-GRS 150 \$1459.00", "HP Pavilion ze1230 \$959.00", "Fujitsu Lifebook C21111 \$1599.00"]}) ), Tr(td("Please specify the number of cars you want:"), td(popup_menu({-name=>"qty", -values=>([1..25])})) ) ), submit({-value=>"Place Order"}), end_form, ("

\"Valid

"), end_html; } # ----------------- Page 2 - Price confirmation page ---------------------------- sub page2 { print start_html(-title=>"Page 2",-bgcolor=>'#CCCC99', -text=>'#808000', -vlink=>' #808000', -alink=>'#808000'), h1("Price confirmation"), p(b("This is your order:")), table({-border=>0}, Tr(td(["Vehicle:",$productdesc])), Tr(td(["Quantity:",$qty])), Tr(td(["Unit Price:",$unitprice])), Tr(td(["Extended Price:",$extprice])), Tr(td(["GST:",$gst])), Tr(td(["PST:",$pst])), Tr(td(["Total:",$ttl])) ); print "
\n"; &showImage; print "
\n", table({-border=>0}, Tr( td( start_form({-method=>"get"}), hidden({-name=>"currentpage",-value=>"2", -override=>1}), hidden({-name=>"product"}), hidden({-name=>"qty"}), submit({-value=>"Continue with order"}), end_form, ), td( start_form({-method=>"get"}), hidden({-name=>"currentpage",-value=>"2", -override=>1}), hidden({-name=>"goto",-value=>"1"}), submit({-value=>"Cancel and return to Catalog"}), end_form, ) ) ), end_html; } # ----------------- Page 3 - Personal Information page ---------------------------- sub page3 { $firsttime=($currentpage ne "3"); print start_html(-title=>"Page 3",-bgcolor=>'#CCCC99', -text=>'#808000', -vlink=>' #808000', -alink=>'#808000'), start_form({-method=>"get"}), h1("Personal Information"), hidden({-name=>"currentpage",-value=>"3", -override=>1}), hidden({-name=>"product"}), hidden({-name=>"qty"}), table({-border=>0}, Tr(td(["Full Name",textfield({-name=>"name"}), (!$valid{name} &&!$firsttime)?p("Error"):""])), Tr(td(["E-mail",textfield({-name=>"email"}), (!$valid{email} &&!$firsttime)?p("Error"):""])), Tr(td(["Phone",textfield({-name=>"phone"}), (!$valid{phone} &&!$firsttime)?p("Error"):""])), Tr(td(["Address line 1",textfield({-name=>"address1"}), (!$valid{address1} &&!$firsttime)?p("Error"):""])), Tr(td(["Address line 2",textfield({-name=>"address2"}), (!$valid{address2} &&!$firsttime)?p("Error"):""])), Tr(td(["City",textfield({-name=>"city"}), (!$valid{city} &&!$firsttime)?p("Error"):""])), Tr(td(["Province",popup_menu({-name=>"province", -values=>[sort(keys(%provpc))]}), (!$valid{province} &&!$firsttime)?p("Error"):""])), Tr(td(["Postal Code",textfield({-name=>"postcode"}), (!$valid{postcode} &&!$firsttime)?p("Error"):""])) ), submit({-value=>"Continue with Order "}), end_form, end_html; } # ----------------- Page 4 - Order Confirmation page ---------------------------- sub page4 { $confirmID = time(); # opening a file by associating a file handle with it. open (FILE_HANDLE1 , ">>./history.txt") or die ("Cannot open file :$! \n"); print FILE_HANDLE1 "$confirmID:$name:$email:$phone: $address1:$address2:$city:$province:$postcode\n\tOrdered: $productdesc:$unitprice:$qty:$extprice:$gst:$pst:$ttl\n\n"; # closing a file handle close (FILE_HANDLE1) or die ("Cannot close file :$!\n"); print start_html(-title=>"Page 4",-bgcolor=>'#CCCC99', -text=>'#808000', -vlink=>' #808000', -alink=>'#808000'), h1("Order Confirmation"), p("Thank you for your order! Your order confirmation number is", b($confirmID),"."), start_form({-method=>"get"}), hidden({-name=>"currentpage",-value=>"4", -override=>1}), hidden({-name=>"goto",-value=>"1",-override=>1}), submit({-value=>"Return to Page1"}), end_form, end_html; }