Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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.


In reply to carrying a lot of vars from page to page with hidden() by scratch

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (6)
As of 2024-04-20 00:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found