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:
scalar( @reqform ) != scalar( $q->param() )
join( $; , sort( @reqform ) ) ne join( $; , sort( $q->param() ) )
while( ( $key, $value) = each %form ) { push @missing_params, $key if $value eq ''; } if( scalar @missing_params ) # ...
#!/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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |