cmic has asked for the wisdom of the Perl Monks concerning the following question:

Hello.
I dare write to Perlmonks (frightening ...)
I want to "cascade" CGI forms but I can't keep the
variable $index (see the example/excerpt below). How come.
Is there any trick ?
Real use : the first submit yields an array. I use this array as a selection list for
the 2nd submit. The 2nd submit yields... nothing !
... my $q=new CGI; my ($index); print $q->header; print $q->start_html(-title=>"Modif", -author=>"cmic" ); print $q->start_form; print $q->submit(-name=>"submit1", -label=>"submit1") ; # if ($q->param('submit1') eq 'submit1') { $index="Some val"; print "DEBUG after 1st submit " . $index . "<br>"; print $q->submit(-name=>"submit2", -label=>"submit2") . " ", } elsif ($q->param('submit2') eq 'submit2') { print "DEBUG after 2nd submit " . $index . "<br>" ; } print $q->end_form; print $q->end_html; ...
-- cmic. Life helps. Perl Too.

Replies are listed 'Best First'.
Re: Cascading CGI forms
by b10m (Vicar) on Apr 15, 2005 at 15:40 UTC

    There are multiple ways you can tackle this

    1. Sessions
    2. Cookies
    3. Hidden inputs

    Try something like this:

    if ($q->param('submit1') eq 'submit1') { $index="Some val"; print "DEBUG after 1st submit " . $index . "<br>"; print $q->hidden('index', $index); print $q->submit(-name=>"submit2", -label=>"submit2") . " ", }

    Later in the script, you can then 'recover' the value with $q->param('index')

    --
    b10m

    All code is usually tested, but rarely trusted.
Re: Cascading CGI forms
by dorward (Curate) on Apr 15, 2005 at 15:41 UTC

    HTTP is stateless, and each request runs the script from scratch.

    If you want persistant data, you have to implement it by storing the value somewhere the script can fetch it from such as in a input field of type hidden, a cookie, or a session.

      Hi PerlMonks. Finally I resolved my problem. First I am now conscious of what "stateless" means. Second, as I have to save a hash of has, I couldn't use the hidden filed technique. Solution: As I was working with Dbm::DEEP, I have created a temporary table with this module, save info in session one, and retrieve it for the following session. It works. I find this technique a bit clumsy, but I think there is no ohter solution. Many thanks to all.
      -- cmic. Life helps. Perl Too.