i think this should be a comprehensive solution to your problem. this folds in suggestions by the other monks, and adds accuracy (which is important).

first off, i've turned on warnings and diagnostics, and disabled output buffering (this last one may be cargo-cult programming, but i always do it by default). i've used the object-oriented style of CGI programming, as it's my preference, and it's faster as per the documentation.

the meat of the error checking algorithm is this:

here's the full code. i must say, i've done all this work, but i haven't tested it. feel free to come down on me if it doesn't work, i deserve it.

#!/usr/bin/perl -w use strict; use diagnostics; $|++; use CGI; my $q = new CGI; my @reqform = qw( var1, var2, var3 ); print $q->header(); if( ( # different number of elements scalar( @reqform ) != scalar( $q->param() ) ) || ( # element names do not match join( $; , sort( @reqform ) ) ne join( $; , sort( $q->param() ) ) ) ) { # print error message for mismatched parameters print $q->start_html(), $q->p( "ERROR: mismatched parameters:" ), $q->p( "required: ", join ' ', sort @reqform ), $q->p( "passed: ", join ' ', sort $q->param() ), $q->end_html(); } # get variables from submitted form my %form = $q->Vars(); # note: this does not work with isindex tags my ( $key, $value, @missing_params ); while( ( $key, $value) = each %form ) { push @missing_params, $key if $value eq ''; } if( scalar @missing_params ) { print error message print $q->start_html(), $q->p( "ERROR: undefined parameter(s): @missing_params" ), $q->end_html(); } # carry on...

~Particle ;Þ

riding the unicode bandwagon...


In reply to Re: Checking Submitted Form Data by particle
in thread Checking Submitted Form Data by Thathom

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.