#buildapizza.cgi #build-a-pizza order form #!/usr/local/bin/perl use CGI; $cgiobject=new CGI; $cgiobject->use_named_parameters; @allparams=$cgiobject->param(); if ($#allparams>-1) {&get_state_variables} else {&init} if ($orderComplete) { &complete_order } else { &build_order } sub build_order() #build the pizza order { unless ($newOrderFlag) { $greeting="Welcome! Please begin building your pizza order." } else { $greeting="<H3>Build-a-Pizza</H3>You may modify or finalize your order below." } $greeting.="<br>Current pizza price: <font color='red'>\$". sprintf("%.2f",&calcTotal())."</font>."; print $cgiobject->header; print $cgiobject->start_html(-title=>'Build-a-Pizza',-bgcolor=>'white'); print "<H2>$greeting</H2>"; &output_form; print $cgiobject->end_html; } sub init() #define initial values for pizza parameters { $name="Your name"; $address="Where to?"; $phone="Your phone"; $deliver="deliver"; $toppings="pepperoni"; $size="large"; } sub get_state_variables() #grab any parameters which were submitted { $name=$cgiobject->param("order_name"); $address=$cgiobject->param("order_address"); $phone=$cgiobject->param("order_phone"); $deliver=$cgiobject->param("order_deliver"); $toppings=$cgiobject->param("order_toppings"); $size=$cgiobject->param("order_size"); $newOrderFlag=$cgiobject->param("newOrderFlag"); $orderComplete=$cgiobject->param("finish") } sub calcTotal() #calculate pizza cost based on current parameters { %sizelabels=('small'=>'small (8",$5)', 'medium'=>'medium (12",$8)', 'large'=>'large (16",$10)', 'xlarge'=>'x-large (18",$12)'); %sizeprices=('small'=>5, 'medium'=>8, 'large'=>10, 'xlarge'=>12); %toppingsprices=('small'=>.50, 'medium'=>.75, 'large'=>1.00, 'xlarge'=>1.25); $baseCost=$sizeprices{$size}; @allToppings=$cgiobject->param("order_toppings"); $toppingsCost=($#allToppings+1)*($toppingsprices{$size}); return $baseCost+$toppingsCost; } sub complete_order() #checkout order { print $cgiobject->header; print $cgiobject->start_html(-title=>'Thank you from Build-a-Pizza',-bgcolor=>'white'); $invoice= "<H2>Thank you for your pizza order!</H2>". "<HR>Deliveries should arrive in 30-45 minutes. ". "Pickup orders please arrive in about 20 minutes.<BR>". "The following order has been sent to the kitchen:<BR>"; if ($deliver eq "deliver") { $invoice.="<B>Delivery</B> " } else { $invoice.="<B>Pickup</B> " } $invoice.="Order for: $name<br>"; if ($deliver eq "deliver") { $invoice.="Delivery to: $address<br>" } $invoice.="Order Description:<br>A <B>$size</B> pizza with the following toppings:<br>"; @allToppings=$cgiobject->param("order_toppings"); $invoice.="<B>".join(",",@allToppings)."</B>"; $invoice.="<BR>TOTAL DUE: ".sprintf("%.2f",&calcTotal()); print $invoice; ##here would be code to send invoice kitchen ##such as by way of email } sub output_form() #construct and output the pizza form HTML { $theform=$cgiobject->startform(-name=>'pizzaform', -method=>'get', -action=>'/cgi-bin/buildapizza.cgi'); #create name text input field $theform.="Your name: "; $theform.=$cgiobject->textfield(-name=>'order_name', -size=>30, -default=>$name); #create phone text input field $theform.="<BR>Your telephone number: "; $theform.=$cgiobject->textfield(-name=>'order_phone', -size=>10, -default=>$phone); #create address text input field if deliver option selected $theform.="<BR>Deliver to address:<BR>"; $theform.=$cgiobject->textarea(-name=>'order_address', -rows=>3, -default=>$address); #create delivery radio buttons $theform.="<BR>"; $theform.=$cgiobject->radio_group(-name=>'order_deliver', -values=>["pickup","deliver"], -default=>$deliver); #create toppings checkboxes $theform.="<HR>Please select toppings from the list below:<BR>"; $theform.="<SMALL>Note: Each topping costs .50,.75,1.00,1.25 for S,M,L,XL pizza</SMALL>"; $theform.=$cgiobject->checkbox_group(-name=>'order_toppings', -values=>['pepperoni','sausage','meatball', 'mushroom','peppers','pineapple', 'ham','shrimp','tomato', 'onion','anchovies','liver'], -default=>$toppings, -linebreak=>'false', -columns=>3); #create size radio buttons $theform.="<BR>Select a pizza size: "; $theform.=$cgiobject->radio_group(-name=>'order_size', -values=>["small","medium","large","xlarge"], -labels=>\%sizelabels, -default=>$size); #create hidden field for newOrderFlag $theform.=$cgiobject->hidden(-name=>'newOrderFlag',-value=>'1'); #create submit and reset buttons $theform.="<BR><BR>"; $theform.=$cgiobject->submit(-name=>"calculate", -label=>'Calculate'); $theform.=$cgiobject->submit(-name=>"finish", -label=>'Finish Order'); $theform.=$cgiobject->endform; print $theform } |