http://qs321.pair.com?node_id=10989

scratch has asked for the wisdom of the Perl Monks concerning the following question: (cgi programming)

Howdy,

I'm working on what's going to grow to be a decent sized (100+) script in which I want to carry all the vars from page to page. I'm using CGI.pm and hidden(), but right now I'm naming all the vars individually on each page.

Every sort of loop that I've tried to read through param() only carries the vars from page 1 to page 2, from page 2 to page 3, etc. It doesn't carry vars from page 1 to page 3.

Is there a better way to do this (make page 1 vars available on page 5, say) than the brute force method of naming them all, over and over on each page?

Here's the code I'm working with:

#!c:\perl\bin\perl.exe -w # Script: p9258.pl use CGI ':all'; use warnings; $action=param('action'); (%allvars); #foreach $name ( param() ) { # $value = param($name); # print "The value of $name is $value\n"; #} #each time we invoke this script we need to test #the value of $action so we know which page to #display next #execute the correct subroutine if ($action eq "Onto page 2") { sub_2(); } elsif ($action eq "Onto page 3") { sub_3(); }else { sub_1(); } #print the right page if ($action eq "Onto page 2") { print_form2(); } elsif ($action eq "Onto page 3") { print_form3(); } else { print_form1(); } #here we've got the subroutines that print the pages. #there's one for each page sub print_form1 { print header, start_html('p9258 Page 1 - Demographics'), h1('Demographic Information'); print start_form, "Please enter your ID: ", textfield(-name=>'ID', -maxlength=>10, ), p, p, "If you've answered the demographic questions on an earlier vi +st please", " press the 'Onto page 2' button at the bottom of this page.", p, p, hr, "Some demog info", p, p, strong("1. "), "What is your sex? ", radio_group(-name=>'sex', -values=>['1', '2'], -labels=>{'1'=> +'male', '2'=>'female'}), p(), strong("2. "), "What is your highest level of education? ", p, popup_menu(-name=>'education', -values=>['1', '2', '3', '4', ' +5', '6', '7'], -labels=>{'1'=>'Less than bachelors', '2'=>'Bachelors in SW', '3'=>'Bachelors in other field', '4'=>'Masters in SW', '5'=>'Masters in other field', '6'=>'PhD or Professional Doctorate', '7'=>'Other'}), p(), hidden(-name=>'sex'), hidden(-name=>'education'), submit(-name=>'action', -value=>'Onto page 2'), end_form; } sub print_form2 { print header, start_html('p9258 Page 2 - Household Member Information'), h1('Household Member Information'); #foreach $name ( param() ) { # $value = param($name); # print "The value of $name is $value\n"; #} foreach $key (keys %allvars) { print "For $key we have a value of $allvars{$key}\n"; } print start_form, "Please tell us who's living in the household:", p, p, strong("10. "), "Is the biological mother in the household now?", p, radio_group(-name=>'bioMom', -values=>['1', '2'], -labels=>{ +'1'=>'Yes', '2'=>'No'}), p(), # hidden(-name=>'ID'), # hidden(-name=>'sex'), # hidden(-name=>'education'), # hidden(-name=>'bioMom'), submit(-name=>'action', -value=>'Onto page 3'), end_form; } sub print_form3 { print header, start_html('Page 3'), h1('THIS IS PAGE 3'); foreach $key (keys %allvars) { print "For $key we have a value of $allvars{$key}\n"; } #foreach $name ( param() ) { # $value = param($name); # print "The value of $name is $value\n"; #} print start_form, "This is page 3: ", p(), submit(-name=>'action', -value=>'Submit completed form'), end_form; } #here we've got the subroutines that are run after #each page is submitted. sub sub_1 { } sub sub_2 { foreach $name ( param() ) { $value=param($name); $allvars{$name} = $value; } # foreach $name ( param() ) { # hidden(-name=>$name); # } } sub sub_3 { # foreach $name ( param() ) { # hidden(-name=>$name); # } foreach $name ( param() ) { $value=param($name); $allvars{$name} = $value; } }

Originally posted as a Categorized Question.