Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions


WDVL Newsletter

Active Server Pages
JSP/Java Servlets
Microsoft SQL Server
Daily Backup
Dedicated Servers
Streaming Audio/Video
24-hour Support    

jobs.webdeveloper.com

Hiermenus


e-commerce
Partner With Us















Developer Channel
FlashKit.com
JavaScript.com
JavaScriptSource
Developer Jobs
ScriptSearch
StreamingMediaWorld
Web Developer's Journal
Web Developer's Virtual Library
WebDeveloper.com
Webreference
Web Hosts
XMLfiles.com

internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


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


Up to => Home / Authoring / Languages / Perl / PerlfortheWeb




Jupiter Online Media: internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and Jupiter Online Media

Jupitermedia Corporate Info


Legal Notices, Licensing, & Permissions, Privacy Policy.

Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers