On Monday 28 Jun 2010 13:55:56 Thomas Bätzler wrote:
> Herb <[email protected]> asked:
> > I've been working on putting together a multi-page form in a single
> > perl script. I have everything working as a single page form (writing
> > to mysql database and emailing the output upon submit) but have not
> > had any luck breaking the form into multiple pages. As mentioned
> > before, I'm not an everyday perl programmer so I may be going about
> > this whole thing wrong.
> >
> > I'm trying to break 20 parameters into 4 pages as:
> >
> > if ($param1 eq "") {
> >
> > <display page1>
> >
> > } elsif ($param6 eq "") {
>
> I'd suggest that you use one CGI parameter for page selection. My personal
> preference is to call it "action" and give it meaningful values, like:
>
> use CGI;
> use CGI::Carp qw/fatalsToBrowser/;
>
> my $q = CGI->new();
>
> [...]
>
> my $action = $q->param('action') || '';
>
> if( $action eq "update" ){
> # display update page
> } elseif( $action eq "insert" ){
> # display insert data page
> } else {
> # display start/login page
> }
1. In Perl, it's "elsif" instead of "elseif".
2. It's better to use a dispatch table in this case:
[code]
my %actions =
(
"update" => \&do_update,
"insert" => \&do_inesrt,
.
.
.
);
if (exists($actions{$act}))
{
$actions{$act}->(@params);
}
else
{
die "No such actions '$act'"; # Be careful of XSS-attacks though!
}
[/code]
3. In the case of a web app, I would recommend against using an HTTP GET
parameter as a page designator and instead use a URL component. I.e: instead
of:
http://myapp.tld/app/?page=settings
Do:
http://myapp.tld/app/settings/
You can look at CGI.pm's path_info() method on how to set it up.
Regards,
Shlomi Fish
>
> Using a single "action" parameter avoids ambiguities when deciding which
> page to display.
>
> HTH,
> Thomas
--
-----------------------------------------------------------------
Shlomi Fish http://www.shlomifish.org/
Understand what Open Source is - http://shlom.in/oss-fs
God considered inflicting XSLT as the tenth plague of Egypt, but then
decided against it because he thought it would be too evil.
Please reply to list if it's a mailing list post - http://shlom.in/reply .
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/