MultiPage Forms Using The Hidden Widget
#!/usr/bin/perl -w

#  The final step towards completion: making sure the data entered by the user 
#  is not nonsense!!  Formatting data into final form!!
#  An additional step -- recovering on formatting errors!

use DBI;
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser); # Makes die work acceptably! Very important!!

print header(), start_html("Saving Intrasession State");
$go = param("go");
$page = param("page");
print p("page is $page and go is $go");
print_page1() if $go eq "Return to Form" || $page eq "" || ($go eq "Previous Page" && $page == 2);
print_page2() if $go eq "Next Page" && $page == 1;
print_page2() if $go eq "Previous Page" && $page == 3;
print_page3() if $go eq "Next Page" && $page == 2;

if ($go eq "Submit Information")
{
  $db = DBI->connect("DBI:CSV:f_dir=.") || 
        die "Cannot connect:  $DBI::errstr\n";

#  Better to create the table definition in the database itself
#  rather than a script.  But here's what it looks like!

   if (!-e "people")
   {
       $cmd = $db->prepare("CREATE TABLE people(
       name varchar(32) not null,
       SSN varchar(32) not null,
       color varchar(32),
       number integer )");
       $cmd->execute() || die "Cannot create table!\n";
    }
# Connect to the database if first time the script is called.
    $name   = param("Name");
    $SSN    = param("SSN");
    $color  = param("color");
    $number = param("Number");
    verify($name, $SSN, $number);

    $row = $db->prepare(
                    "INSERT INTO people (name, SSN, color, number)
                     VALUES            ('$name', '$SSN', '$color', '$number')");

#  Run the prepared insert statement.
   $row->execute();
   $statement = "SELECT * from people";
      $doit = $db->prepare($statement);
      $doit->execute();
      while (($name, $SSN, $color, $number) = $doit->fetchrow_array)
             {print p("data: $name, $SSN, $color, $number\n\n")}
      $db->disconnect() || die "Cannot disconnect\n";
}
print end_html;


sub print_page1
{
    print start_form,
          p("Name: "),
          textfield(-name => "Name", -size => 50),
          p("Social Security Number: "),
          textfield(-name => "SSN", -size => 9),
          reset(-name => "Reset"),
          submit(-name => "go", -value => "Next Page"),
          hidden(-name => "page", -value => "1", -override => 1),
 #  Parameter values for color and Number saved if they exist!!
          hidden(-name => "color"), hidden(-name => "Number"),
          end_form;
} 


sub print_page2
{
    print start_form,
          p("Favorite Color"),
          radio_group(-name => 'color',
                      -value => ['blue', 'red', 'green', 'yellow', 'purple',
                                 'orange', 'white', 'black', 'brown'],
                      -default => param("color")),
          p("Favorite Number"),
          textfield(-name => "Number", -size => 3, -default => param("Number")),
          reset(-name => 'Reset'), 
          submit(-name => "go", -value => 'Previous Page'),
          submit(-name => "go", -value => "Next Page"),
          hidden(-name => "page", -value => "2", -override => 1),
          hidden(-name => "SSN"), hidden(-name => "Name"),
          end_form;
}



sub print_page3
{
    my (@rows, $field);

    print start_form;
    foreach $field (qw(Name SSN color Number))
    {
        push(@rows, Tr(th({-align => LEFT}, $field), td(param($field))));
        print hidden(-name => $field);
    }

    print table(@rows), 
          hidden(-name => "page", -value => 3, -override => 1),
          submit(-name => "go", -value => "Previous Page"),
          submit(-name => "go", -value => "Submit Information"),
          submit(-name => "go", -value => "Save Session");

          create_hidden();
          print end_form;
}


sub create_hidden
{
     foreach ('Name', 'SSN', 'color', 'Number')
     {
         print hidden(-name => $_);
     }
} 



sub verify
{
     my ($name, $SSN, $number) = @_;
     my ($err) = 0;

     print (p("Illegal Name!")), $err = 1
            if $name   !~ /^\s*[a-z]+\s+[a-z]+\s*$/i;
     print (p("Illegal Social Security Number!!\n")), $err = 1 
            if $SSN    !~ /^\d{9}$/;
     print (p("Illegal Favorite Number!!\n")), $err = 1
            if $number !~ /^\s*\d{1,3}\s*$/;
     if ($err)
     {
         print start_form;
         create_hidden;
         print submit(-name => "go",  -value => "Return to Form");
         print end_form; 
         print end_html;
         exit;
     }
}