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


Single-Session State Maintenance AKA "Building a Pizza" Scenario

June 14, 1999

The wizened pizza parlor entrepreneur knows that a great many pizzas are ordered by computer jockeys, and that most computer jockeys prefer to do, well, anything from the keyboard. So, building a pizza parlor on the Web isn't as ludicrous an idea as it may sound.

To this end, we've conjured up a little pizza joint-in-a-CGI-script -- one script is all it takes, and in doing so we'll see how to maintain state while the hungry shopper hems and haws over various pizza options. Starting with a visual aid, the basic template for our parlor looks as follows:

The parlor's operation is straightforward: the customer fills in the text fields, selects pickup or delivery, and chooses toppings and a pizza size. When "Calculate" is clicked, the page reloads reflecting the current options and the current price. Any options can be changed, "Calculate" may be re-clicked, and an updated display will appear. This may go on infinitely, until one clicks "Finish Order", wherein a final invoice is generated and, presumably, the order will be sent onto the kitchen.

As we walk through the code to buildapizza.cgi, which is somewhat longer than our usual examples, you may want to consult a window or printout containing the entire script. Or test drive the live example of Build-A-Pizza (for fun only, no real pizzas are baked here!).

#build-a-pizza order form
#!/usr/local/bin/perl
use CGI;
$cgiobject=new CGI;
$cgiobject->use_named_parameters;

Beginning in familiar territory, the CGI environment is pumped and primed for the remainder of this Perl program.

@allparams=$cgiobject->param();
if ($#allparams>-1)
 {&get_state_variables} 
else 
 {&init}

The script must detect whether the user is submitting modified form fields or whether this is the beginning of a new order (no form fields submitted). Above, we grab an array of all parameters submitted and check whether there were any at all (a list length of -1 represents no parameters). If any parameters were submitted then we branch into the get_state_variables subroutine which will read the submitted values and populate the appropriate variables. For a new order, we branch to init which sets the initial values for the pizza building variables.

if ($orderComplete)
 { &complete_order }
else 
 { &build_order }

We'll see later that the variable $orderComplete represents whether the "Finish Order" submit button was clicked. If so, we want to branch to the invoice subroutine complete_order; otherwise it was the "Calculate" button which submitted the form and we wish to update the page via build_order.

Final Cookie Thoughts
The Perl You Need to Know
Building a Pizza: Taking the Order


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