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

Hi monks

i'm trying to make a multi page form using CGI.pm

and i'm facing a problem with switching to the thired step

#!/usr/bin/perl -w use strict; use CGI ":standard"; print header; my $action = param('action'); my $title = 'Simple Script'; if(not $action){ print start_html(-title=>$title); print '<a href="?action=add">Start</a>',end_html; }elsif($action eq "add"){ print start_html(-title=>$title.' Enter your name'); print start_form(-action=>"?action=addnew"), textfield('name','Enter Your name',20), submit('submit'),reset, end_form,end_html; }elsif($action eq "addnew"){ my $name = param('name'); if(not $name){ print p('Error , You should write your name'); }else{ print p("Welcome $name"); } }

by the way it's my first time using CGI.pm i was using the old way of reading from STDIN

Replies are listed 'Best First'.
Re: Multi page form
by Purdy (Hermit) on Sep 28, 2005 at 14:16 UTC
    Sounds like you're evolving - I highly recommend proceeding the next step & evolving to a MVC CGI framework, like CGI::Application - it would work something like this:
    #!/usr/bin/perl -wT # this would be your .cgi file... let's say name.cgi use strict; use CGI::Carp qw( fatalsToBrowser ); $|++; use MyApp; my $webapp = MyApp->new(); $webapp->run();
    Then a complementing cgiapp module:
    package MyApp; use strict; use base 'CGI::Application'; sub setup { my $self = shift; my $self->run_modes( [ qw( start add addnew ) ] ); } sub start { my $self = shift; my $query = $self->query; my $output = $query->start_html( -title => 'Simple Script' ); $output .= '<a href="name.cgi?rm=add">Start</a>' . $query->end_html; return $output; } sub add { my $self = shift; my $query = $self->query; my $output = $query->start_html(-title=>$title.' Enter your name'); $output .= $query->start_form . $query->hidden( 'rm', 'addnew' ) . $query->textfield('name','Enter Your name',20) . $query->submit('submit') . $query->reset . $query->end_form . $query->end_html; return $output; } sub addnew { my $self = shift; my $query = $self->query; my $text = 'Error , You should write your name'; if ( $query->param( 'name' ) ) { $text = "Welcome $name"; } my $output = $query->p( $text ); return $output; } 1;
    HTH & Good Luck! - Jason

    Update: Feel free to drop in on the #cgiapp channel on irc.perl.org
    Update2: bugfix ... that's not really the best code -- just something I whipped up as a prototype

      Thanks for your reply

      i have tryed your example and still facing the same problem it doesn't switch to the 3rd step.

      stuck in the 2nd page

        Did you get my latest code?

        In regards to your original code, my guess is that action parameter for your form is invalid. I've never done it that way - I've instead used hidden fields. So in your $action eq 'add' branch, set the action of the form to the name of your cgi script. Then add a hidden field:

        # ... }elsif($action eq "add"){ print start_html(-title=>$title.' Enter your name'); print start_form(-action=>"myscript.cgi"), # is that the name? hidden( 'action', 'add' ), textfield('name','Enter Your name',20), submit('submit'),reset, end_form,end_html; # ...

        HTH & Have Fun!

        - Jason

        PS: Drop by the #cgiapp channel & we can help you further...

Re: Multi page form
by kwaping (Priest) on Sep 28, 2005 at 14:37 UTC
Re: Multi page form
by eric256 (Parson) on Sep 28, 2005 at 15:35 UTC

    The part that is causing you trouble is subtle. By default CGI.pm only excepts parameters from the POST *OR* the GET. This means that it isn't getting the value you tacked onto the end of the URL, rather it is only getting the form elements submited. I've never decided if this is a bug or feature of CGI.pm and once i changed the way it worked. However then you upgrade and forget and wonder why all your CGI's stop working! So the easy solution is to move the action aout of the form action attribute and into a hidden form element instead.


    ___________
    Eric Hodges