Building a Pizza: Updating the Order
June 14, 1999
Only one subroutine remains of
buildapizza.cgi,
and that is output_form which is actually quite
similar to the output_form we saw from the cookie
creation example. Alas, it is also quite long.
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
}
As we saw earlier, this subroutine sequentially outputs
each form field. The starting values for each field
are set to the variables which are plugged into the CGI
object methods; for example, the "order_name"
text field begins with the value in $name, which
itself comes from the data in this text field from a previous
submission (or else, if a new order, from the default
values supplied by init).
Some of these fields are inherently complex to construct --
for example, the radio button group which selects
the pizza size. The four buttons in this group have a set
of labels, which appear on screen, and a different set
of values, which are submitted with the form. The values
are assigned on-the-fly, as seen in the line:
-values=>["small","medium","large","xlarge"]
However, the labels which appear onscreen are defined
in the hash named %sizelabels which we previously
defined in the calcTotal subroutine.
Despite the tangle of code, there is beauty within:
the magic is that each time the visitor makes changes to
the form options, and clicks the "Calculate"
button, our script "remembers" the whole set of
options. Thus, the user may enter his name and address
and a set of toppings, but then change his mind about the
pizza size. He may modify the pizza size, and even the
toppings, and when he resubmits the page, our script remembers
the user's name and address. And that is the moral of
our story: maintaining state!
What's more, this technique for maintaining state is
handled entirely by the CGI module itself. Which is why,
when you need to maintain state within a session,
employing the CGI module as illustrated obviates the need to
deal with cookies.
Building a Pizza: Completing the Order
The Perl You Need to Know
Final Thoughts
|