Yes, there is a way to check if a form was submitted via CGI. You need three things:

  1. a way to generate the HTML form
  2. a way to accept the submission of that form
  3. a way to generate the results of submitting that form
By using template generation (such as HTML::Template) you can knock that down to two steps: display some HTML and deal with submitted form parameters.

The trick is to use the presence of the "submit button's" parameter: if it is not found in the query parameters then the client has not submitted the form, so you display the form. If the "submit button's" presence is found in the query then you display results (or form submission errors that need to be corrected). Templates are smart in that they also have conditionals, so you can put the logic there instead of adding more code to your CGI script.

Here is an example using HTML::Template and Plack (via Plack::Request and Plack::Response). You run this via plackup and what's ultra nice is that you can just drop this code into an Apache(2) enabled cgi-bin directory (use a .cgi extension) and it runs like you would expect any old .cgi script to. (See Plack::Handler::CGI.)

use strict; use warnings; use Plack::Request; use Plack::Response; use HTML::Template; my $app = sub { my $tmpl = HTML::Template->new( filehandle => \*DATA, die_on_bad_params => 0, ); my $req = Plack::Request->new( shift ); my $query = $req->parameters; $tmpl->param( $query ); my $res = Plack::Response->new( 200 ); $res->content_type('text/html'); $res->body( $tmpl->output ); $res->finalize; }; __DATA__ <tmpl_if submitted> <h1>Hello <tmpl_var textfield> <tmpl_var textfield2></h1> <tmpl_else> <form> <p>firstname: <input type="text" name="textfield" /</p> <p>lastname: <input type="text" name="textfield2" /</p> <p><input type="submit" name="submitted" value="Submit" /></p> </form> </tmpl_if>

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

In reply to Re: cgi check button by jeffa
in thread cgi check button by bigup401

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.