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

I am new to Perl still, and have basically started on CGI.pm recently. I'm not going to get into anything *big* right now on it, as I don't want to get in head deep *yet* *lol*. Anyway, my question is this. How would I start off on multiple forms? Basically I want one page where you can (for example purposes) input your name. That name would be stored in param('name'). After submitting that, the script would then clear the screen (if not, that's fine) and another form would come up with (again for example purposes) an age field. I would require several of these, and then some choice results at the end (which I could manage).

I've been fooling around with some of these forms, but I can't get them working the way I'm needing them =( I could easily do this without CGI.pm, but after some discussion here (and with some friends), I'm moving to it.

Any help would be appreciated (even a source to read up on this properly). Thanks.

Replies are listed 'Best First'.
Re: CGI.pm multiple forms
by Zaxo (Archbishop) on Nov 22, 2002 at 04:12 UTC

    You can give each form's submit buttons a single name, to check for which form is shipped, and different values to determine what function is meant. So a login form might have Login=Sign Up, Login=Login, or Login=Logout, while the blog poster form has Node=Preview or Node=Stumbit.

    Or else you can set different actions for each form.

    After Compline,
    Zaxo

Re: CGI.pm multiple forms
by jaldhar (Vicar) on Nov 22, 2002 at 04:55 UTC

    Sorting out all the various states you can get with multiple forms can get messy really quickly if you try and handle it by yourself. You are better off using a module like CGI::Application which gives you a nice architecture for this type of thing.

    Basically, your script becomes a small stub which just contains configuration information and the bulk of the code is in one or more modules which are made of subroutines which represent 'run-modes' or pages in yourapplication. You can navigate from page to page by calling different run-modes and store state in between. As a bonus it also has hooks into HTML::Template to keep your business logic seperate from your HTML. And you can use any of the functions from CGI.pm.

    --
    જલધર

Re: CGI.pm multiple forms
by dingus (Friar) on Nov 22, 2002 at 09:19 UTC
    Some good suggestions already. Here's a couple more that you cna use instead of/as well as:

    Some ways you can keep track of which stage you are at in your script

    • use a hidden variable or set of variables.
    • use a cookie
    • use the different button name method suggested above
    • set additional path_info: e.g. URLs like http://www.site.com/cgi-bin/myscript.cgi/stage1 and http://www.site.com/cgi-bin/myscript.cgi/stage2
    Alternatively you can create separate cgi scripts for stage 1,2 etc. (mystage1.cgi, mystage2.cgi) and make sure that the form created in stage on has the action to invoke stage 2 (use start_form(-action=>'myscript2.cgi') - you need to do that for the path_info technique as well)

    Dingus


    Enter any 47-digit prime number to continue.
Re: CGI.pm multiple forms
by rdfield (Priest) on Nov 22, 2002 at 09:54 UTC
    merlyn wrote a web column on this particular topic. Have a look at this.

    rdfield

      Thanks for all the help =) I'll take a look at some of these methods.
Re: CGI.pm multiple forms
by reclaw (Curate) on Nov 23, 2002 at 07:05 UTC
    I wrote this while showing a neighbor kid the same thing. It's odd, but it might help you get started.
    #!perl -wT use strict; use CGI; my $query = CGI->new; my @fields = qw(Name Age Sex); print $query->header, $query->start_html('Multi page form'), $query->start_form; print_form(); print $query->end_form, $query->end_html; sub print_form { my $count = @fields; my $results; foreach my $field (@fields) { if ($query->param($field)) { print $query->hidden(-name=>$field); $results .= $query->p( $query->b("$field:"), $query->param($field) ); $count--; } else { print $query->b("$field:"), $query->textfield(-name=> $field), $query->submit(-value=> 'Submit'); last; } unless ($count) {print $results} } }