#!/usr/bin/perl #*** This Example Web Site will demonstrate how to save "state" #*** variables betweeen multiple pages. The user will enter data #*** on several pages using forms, anchors, and imagemaps. #*** Four variables will be saved: #*** 1: Name (Entered on a Form) #*** 2: Favorite number (Entered on a Form) #*** 2: Favorite color (Clicked on a particular Hypertext Anchor) #*** 4: Favorite City (Imagemap) #****************************************************************** #******************** Tables ************************************* #****************************************************************** #****************************************************************** #******************** Subroutines ******************************** #****************************************************************** #****************************************************************** #**************** Show USA Map *********************** #****************************************************************** sub CITY_PAGE { print("Content-Type: text/html\n\n"); print("Cities Page\n"); print("\n"); &IMAGE_MAP; &SHOW_INFO_BOX; print "
Please Click on the Blue city name.
\n"; print "\x22an\n"; print "\n"; &SHOW_GO_BAR; print(""); print(""); } #*********************************************************************** #************** Send Back the Name input page ********************** #*********************************************************************** sub COLOR_PAGE { print("Content-Type: text/html\n\n"); print("Color Page\n"); print("\n"); &SHOW_INFO_BOX; print "Please pick you favorite color:\n"; print "
\n"; print "\n"; print "Blue\n"; print "\n"; print "Red\n"; print "\n"; print "Green\n"; print "\n"; print "Yellow\n"; &SHOW_GO_BAR; print(""); print(""); } #*********************************************************************** #************** Send Back the Number input page ********************** #*********************************************************************** sub NUMBER_PAGE { print("Content-Type: text/html\n\n"); print("Favorite Number Page\n"); print("\n"); &SHOW_INFO_BOX; print "
Whats your favorite number:\n"; print "
\n"; print ""); print "\n"; print""; print""; print""; print""; print "
\n"; print "
\n"; print "\n"; print "
\n"; &SHOW_GO_BAR; print(""); print(""); } #*********************************************************************** #************** Send Back the Name input page ********************** #*********************************************************************** sub NAME_PAGE { print("Content-Type: text/html\n\n"); print("Main Page\n"); print("\n"); &SHOW_INFO_BOX; print "
Name\n"; print""; print""; print""; print""; print "\n"; print "
\n"; &SHOW_GO_BAR; print"\n"; print(""); print(""); } #*********************************************************************** #*** Accept passed parameters, and print them out in the same ** #*** format that would be returned in STDIN from a FORM (Post method) ** #*********************************************************************** sub PRINT_SAVE_VARIABLES { print "save,"; print "page=", $_[0], "&"; print "name=", $_[1], "&"; print "color=", $_[2], "&"; print "number=", $_[3], "&"; $_[4] =~ s/\x20/\x2b/g; #replace any blanks in city name with +'s print "city=", $_[4]; } #********************************************************************* #*** Put "button bar" of clickable gifs, that act as hypertext ** #*** anchors ** #********************************************************************* sub SHOW_GO_BAR { print "
\n"; print "\n"; print "\n"; print "\n"; print "\n"; if ($name ne "" && $color ne "" && $number ne "" && $city ne "") { print "\n"; } print "
"; print "See the Perl Source Code"; print " (juggle.cgi) for these pages\n"; print "
"; print "
Back to Examples Page\n"; } #****************************************************************** #**************** Process IMAGEMAP output *********************** #****************************************************************** sub IMAGE_MAP { if ($x != 0 && $y != 0) { $city = ""; } if ($x > 46 && $x < 92 && $y > 13 && $y < 26 ) { $city = "Seattle"; } if ($x > 39 && $x < 115 && $y > 106 && $y < 122 ) { $city = "Los+Angelos"; } if ($x > 208 && $x < 275 && $y > 64 && $y < 78 ) { $city = "Chicago"; } if ($x > 417 && $x < 491 && $y > 78 && $y < 89 ) { $city = "New+York"; } if ($x > 367 && $x < 421 && $y > 186 && $y <201 ) { $city = "Miami"; } } #******************************************************* #************ Send Back test Data Page ***************** #******************************************************* sub DEBUGGING_PAGE { print("Content-Type: text/html\n\n"); print("Debugging Page\n"); print("\n"); print "

Heres what was passed to this CGI program

\n"; print "
\n"; print("

Script Name

",$ENV{SCRIPT_NAME},"
\n"); print("Server Software
",$ENV{SERVER_SOFTWARE},"
\n"); print("SERVER_NAME
",$ENV{SERVER_NAME},"
\n"); print("Gateway interface
",$ENV{GATEWAY_INTERFACE},"
\n"); print("server protocol
",$ENV{SERVER_PROTOCOL},"
\n"); print("Server Port
",$ENV{SERVER_PORT},"
\n"); print("Remote addr
",$ENV{REMOTE_ADDR},"
\n"); print("Remote Host
",$ENV{REMOTE_HOST},"
\n"); print("Remote User
",$ENV{REMOTE_USER},"
\n"); print("Content Type
",$ENV{CONTENT_TYPE},"
\n"); print("Content Length
",$ENV{CONTENT_LENGTH},"
\n"); print("HTTP Accept
",$ENV{HTTP_ACCEPT},"
\n"); print "Path info
", $ENV{PATH_INFO},"
\n"; print "Path translated
", $ENV{PATH_TRANSLATED},"
\n"; print "Script Name
", $ENV{SCRIPT_NAME},"
\n"; print "Query String
", $ENV{QUERY_STRING}, "
"; print "path translated = "; print $pathtr_string; print"
\n"; print "Query string = "; print $query_string; print"
\n"; print " X= ", $x, " / Y = ", $y; print"
\n"; print"\n"; print(""); print(""); } #******************************************************* #****** Get "state" info passed up from Browser ******** #******************************************************* sub GET_STATE_INFO { #** Look for imagemap x,y data in the Query String ($x, $y) = split(/,/,$ENV{QUERY_STRING}); #** Our Saved Variables can be passed to this program in 2 ways: #** 1: In the URL of an anchor the user clicked on. #** 2: In "Standard In" when they user clicked on the SUBMIT button #** of a form. Any variables that aren't entered on that form #** are passed as HIDDEN Variables. #** When putting our variables in the URL, we put them in the same #** format we would see if they came from a FORM (post method). #** This common format allows us to processs the string of variables #** the same, no-matter its source. #** Is there anything in "Standard In" ? #** if ($ENV{CONTENT_LENGTH} ne "") { read(STDIN, $save_string, $ENV{CONTENT_LENGTH}); # Yes- Use it } else # No- Use whats in URL { $tmp_string = $ENV{PATH_TRANSLATED}; $tmp_ptr = rindex($tmp_string,"save"); #find marker where our data starts $tmp_ptr = $tmp_ptr + 5; #increment past marker $lensave = length($tmp_string) - $tmp_ptr; $save_string = substr($tmp_string,$tmp_ptr,$lensave); } @prompts = split(/&/,$save_string); foreach (@prompts) { ($tmp1, $tmp2) = split(/=/,$_); $tmp2 =~ s/\x2b/\x20/g; $tmp2 =~ s/%2C/\x2c/g; $tmp2 =~ s/%28/\x28/g; $tmp2 =~ s/%29/\x29/g; $fields{$tmp1}=$tmp2; #print "
\n", $tmp1, "=", $fields{$tmp1}, ","; } $page = $fields{'page'}; $name = $fields{'name'}; $color = $fields{'color'}; $number = $fields{'number'}; $city = $fields{'city'}; } #******************************************************* #******************** Do info Box ********************* #******************************************************* sub SHOW_INFO_BOX { print "\n"; print "\n"; print "\n"; print "\n"; print "\n"; if ($name eq "") { print "\n";} else { print "\n"; } if ($color eq "") { print "\n"; } else { print "\n"; } if ($number eq "" ) { print "\n"; } else { print "\n"; } if ($city eq "") { print "\n"; } else { $city =~ s/\x2b/\x20/g; #before display convert +'s to spaces print "\n"; } print "
NameFavorite ColorFavorite NumberFavorite City
", "?", "", $name, "", "?", "", $color, "", "?", "", $number, "", "?", "", $city, "
\n"; } #******************************************************* #************ Send Back Main Screen **************** #******************************************************* sub MAIN_PAGE { print("Content-Type: text/html\n\n"); print("Main Page\n"); print("\n"); &SHOW_INFO_BOX; print "

These pages demonstrate gathering data from the user on one page"; print " and, carrying it over to subsequent pages. As data is entered on"; print " each page it will appear in the table at the top"; print " Click on a button at the bottom to answer each question."; print " You can click on the buttons in any order, and change each "; print "selection as often as you wish. Click on \x22Report\x22 to see "; print "a print out of everything you've entered.

"; &SHOW_GO_BAR; print"\n"; print(""); print(""); } #******************************************************* #************ Send back Report Page **************** #******************************************************* sub REPORT_PAGE { print("Content-Type: text/html\n\n"); print("Report Page\n"); print("\n"); print "

Heres everything you've entered:

"; print "Hello, ", "", $name, ""; print "- your favorite color is ", "", $color, "" ; print ", your favorite number is ", "", $number, ""; print " and, your favorite city is ", "", $city, ""; &SHOW_GO_BAR; print"\n"; print(""); print(""); } #******************************************************* #******************** Main Rtn ************************ #******************************************************* &GET_STATE_INFO; if ($page eq "" || $page == 0 ) { &MAIN_PAGE; goto quit; } if ($page eq "1") { &NAME_PAGE; goto quit; } if ($page eq "2") { &COLOR_PAGE; goto quit; } if ($page eq "3") { &NUMBER_PAGE; goto quit; } if ($page eq "4") { &CITY_PAGE; goto quit; } if ($page eq "5") { &REPORT_PAGE; goto quit; } &DEBUGGING_PAGE; quit: