in reply to cgi check button

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)