Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

carrying a lot of vars from page to page with hidden()

( #10989=categorized question: print w/ replies, xml ) Need Help??
Contributed by scratch on May 10, 2000 at 20:24 UTC
Q&A  > CGI programming


Description:

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; } }

Answer: carrying a lot of vars from page to page with hidden()
contributed by infoninja

AFAIK, there are really only two ways to accomplish this task:

  • Carry the hidden variables through on each page (what you're currently trying to avoid)
  • Implement some other method of preserving state (i.e., cookies, etc)
This is due to the fact that the web browser/server connection is stateless, so you have to do one of the above in order to preserve the state of the variables.
Answer: carrying a lot of vars from page to page with hidden()
contributed by merlyn

go see my column on handling multipage forms, which automates 90% of this for you.

Answer: carrying a lot of vars from page to page with hidden()
contributed by chromatic

Another option is to maintain the state via some method on the server, and only pass a session ID to and from the client. You could use a hash key (and implement some caching mechanism so that they expire after so many minutes) to an internal memory structure (if you use a persistent CGI application), or a unique ID into a database table or a temporary file.

You could store this session ID in a cookie or as a hidden field.

Answer: carrying a lot of vars from page to page with hidden()
contributed by ducky

I would use Apache::Session (which doesn't really need Apache) and pretty much follow the example in the docs. The constraint is that it'll be stuffed into a hash. But do what you wish to the hash... when the next process picks up the session you'll have your data back for that session id.

-Ducky

Answer: carrying a lot of vars from page to page with hidden()
contributed by mezhaka

Use CGI::Session module.

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others rifling through the Monastery: (10)
    As of 2014-07-10 10:23 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      When choosing user names for websites, I prefer to use:








      Results (206 votes), past polls