A CGI program accepts inputs from the browser, processes them in some way and then produces an output (which is usually an HTML page to be displayed by the browser).

There is no reason why the HTML produced by the CGI program can't include another form which will then call another CGI program (or even the same CGI program again).

If you're calling the same CGI program to handle different stages of a process, then you need to give it some way to distinguish which stage each call needs to process. The easiest way to do this is to have a hidden input (I usually call it 'mode') on the form.

I often find myself writing CGI programs that look a bit like this:

#!/usr/bin/perl use strict; use warnings; use CGI ':cgi'; use Template; # for producing output. # List of subroutines that handle the different modes my %modes = ( mode1 => \&process_mode1, mode2 => \&process_mode2, default => \&process_default, ); # Work out which mode we're in my $mode = param('mode'); # If we have a mode, then call that subroutine if ($mode && %modes{$mode}) { $modes{mode}->(); } else { # Otherwise call the default handler $modes{default}->(); } sub process_default { # no mode give, display the default form } sub process_mode1 { # process mode1 and display the next form } sub process_mode2 { # process mode2 and display the next form }

The output templates are either stored in external files, or in the DATA section using Inline::Files.

Of course, these days, you're probably better off using a framework like Catalyst.

Update: Fixed problems noted by blazar below.


In reply to Re: Multiple forms in one CGI file? by davorg
in thread Multiple forms in one CGI file? by lakeTrout

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.