#!/usr/local/bin/perl -Tw use strict; $| = 1; use CGI ":all"; ### configuration ## all-capital names are used in code, so don't change them ## type of default fields unless overridden with $_[0] is fieldname sub DEFAULT_FIELD { textfield(shift, "", 60) } my @PAGES = ( ## first and second pages have no back buttons ["Introduction", \&intro_page], ["Tell us about yourself", [Name => "Name"], [Address => "Address"], [City => "City"], [State => "State"], [Zipcode => "Zip Code"], ], ["Tell us about your experience", [Movie => "Movie you saw"], [Review => "What you thought of it", sub { radio_group(shift, ['No opinion', qw(Excellent Good Fair Poor Bombed)]); }], ], ["Tell us anything else", [Theatre => "Theatre conditions"], [Snackbar => "Snack bar"], [General => "Any other comments?", sub { textarea(shift) }], ], ["Thank you!", \&thank_you_page], ["Goodbye!", \&submit_page], ## last page has no back or forward buttons ); ## Internal use: these must not collide with fieldnames above my $PREVIOUS = "Previous Page"; my $NEXT = "Next Page"; my $PAGE = "__page__"; sub intro_page { print p(["Thank you for visiting ScratchySound Theatres!", "Please take a moment to tell us about your experience.", ]); } sub thank_you_page { print p(["Thank you for taking the time to fill out our survey!", "Go back now to change any entries you need, or forward to send to us!", ]); } sub submit_page { ## code to save params would go here ## be sure to ignore param($PAGE), param($NEXT), param($PREVIOUS) print p(["Your entry has been submitted to us!", "Thank you!"]); print $CGI::Q->dump; # debugging } ### end my $previous = param($PREVIOUS); my $next = param($NEXT); my $page = param($PAGE); if (defined $page and $page =~ /^\d+$/ and $page >= 0 and $page <= $#PAGES) { $page += defined($previous) ? -1 : +1; # default to forward } else { $page = 0; } param($PAGE, $page); # set it for hidden my @info = @{$PAGES[$page]}; my $title = shift @info; print header, start_html($title), h1($title); print start_form; if (ref $info[0] eq "CODE") { $info[0]->(); } else { print table({ Border => 1, Cellpadding => 5 }, map { Tr( th($_->[1]), td(($_->[2] || \&DEFAULT_FIELD)->($_->[0])) )} @info ); } ## no backing up from first or second or last page: print submit($PREVIOUS) if $page > 1 and $page < $#PAGES; ## no going forward from last page: print submit($NEXT) if $page < $#PAGES; ## generate hidden fields print hidden($PAGE); for my $other (0..$#PAGES) { next if $other == $page; # don't dump hiddens for current my @info = @{$PAGES[$other]}; shift @info; # toss title next if ref($info[0]) eq "CODE"; # code page for (map {$_->[0]} @info) { print hidden($_) if defined param($_); } } print end_form; print end_html;