Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Checking Submitted Form Data

by Thathom (Acolyte)
on Feb 27, 2002 at 11:37 UTC ( [id://147864]=perlquestion: print w/replies, xml ) Need Help??

Thathom has asked for the wisdom of the Perl Monks concerning the following question:

Hi, Trying to create a little script that can be set up easily for use again and again for checking submitted Form Data. This works, but I just wanted to know if theres anything I could make more efficient?
#!/usr/bin/perl use CGI; use strict; my $q = new CGI; print "Content-type: text/html\n\n"; #GET VARIABLES FROM SUBMITTED FORM ############################################ my %form = (var1 => $q->param('var1'), var2 => $q->param('var2'), var3 => $q->param('var3')); #SET ARRAY WITH REQUIRED FIELDS ############################################ @reqform = (var1,var2,var3); #CHECK THAT REQUIRED FIELDS FILLED else PRINT ERROR ################################################### foreach (@reqform) { if ($form{$_} eq '') {@errormsg = (@errormsg, $_);} } if(@errormsg) { &PrintError;} else {&CARRY_ON_PROCESSING_FORM; } #PRINT ERROR ################################################### sub PrintError { print "<html><body>"; foreach (@errormsg) { print "You need to enter something for <b>$_</b><br>"; } }
ThAtH0M

Replies are listed 'Best First'.
Re: Checking Submitted Form Data
by gellyfish (Monsignor) on Feb 27, 2002 at 12:39 UTC

    You might consider changing your :

    my %form = (var1 => $q->param('var1'), var2 => $q->param('var2'), var3 => $q->param('var3'));
    to:
    my %form = $q->Vars();
    for a spot of maintainer efficiency :)

    /J\

Re: Checking Submitted Form Data
by projekt21 (Friar) on Feb 27, 2002 at 11:45 UTC

    I'm puzzled that you say, it works, as I get errors:

    Global symbol "@reqform" requires explicit package name at test017.pl +line 16. Global symbol "@reqform" requires explicit package name at test017.pl +line 21. Global symbol "@errormsg" requires explicit package name at test017.pl + line 22. Global symbol "@errormsg" requires explicit package name at test017.pl + line 22. Global symbol "@errormsg" requires explicit package name at test017.pl + line 24. Global symbol "@errormsg" requires explicit package name at test017.pl + line 34. Bareword "var1" not allowed while "strict subs" in use at test017.pl l +ine 16. Bareword "var2" not allowed while "strict subs" in use at test017.pl l +ine 16. Bareword "var3" not allowed while "strict subs" in use at test017.pl l +ine 16. test017.pl had compilation errors.
    change
    my @reqform = qw(var1 var2 var3); # quote them!
    and add
    my @errormsg;

    alex pleiner <alex@zeitform.de>
    zeitform Internet Dienste

Re: Checking Submitted Form Data
by Ido (Hermit) on Feb 27, 2002 at 12:27 UTC
    I think you should call your subs as: PrintError() and CARRAY_ON_PROCESSING_FORM() As the &sub way to call a sub passes @_ as arguments and disables prototype checking, as described in perlsub. Also, I would pass @errormsg to PrintError as arguments. PrintError(@errormsg) And then in PrintError:  foreach(@_){}
Re: Checking Submitted Form Data
by particle (Vicar) on Feb 27, 2002 at 14:28 UTC
    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:

    • error if number of passed parameters does not match number of required parameters (this assumes the all required parameters, and only required parameters, should be passed -- your logic may vary)
      scalar( @reqform ) != scalar( $q->param() )
    • error if passed parameter names do not match required parameter names
      join( $; , sort( @reqform ) ) ne join( $; , sort( $q->param() ) )
    • error if required parameters are missing values
      while( ( $key, $value) = each %form ) { push @missing_params, $key if $value eq ''; } if( scalar @missing_params ) # ...
    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...

      Thanks everyone!! Im going to take all these tips and stick them together. Im still learning but getting there :) ThAtH0M
Re: Checking Submitted Form Data
by Dog and Pony (Priest) on Feb 27, 2002 at 12:32 UTC
    One minor detail - since you are useing CGI.pm, you might as well replace print "Content-type: text/html\n\n"; with the cross-platform, OO, more correct and stylish print $q->header(); *grin*. Even though, as was pointed out to me in this node, it usually doesn't matter, it is the right thing to do. :)
    You have moved into a dark place.
    It is pitch black. You are likely to be eaten by a grue.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://147864]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2024-04-23 11:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found