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

I've been fiddling with this for a while but I still can't figure this out. I hav a form with like 10 different fields on and I want to check to see if $name exists and if $email exists. But somewhere in my logic, I can't get it to error out if they're missing. I can't get the if's/else's to match up. Can someone help me out?
if (param()) { if($name) { if($email) { if(exists $emails{$email}) { print "<center><b>Email already exists in database +.</b></center>\n"; } else { $emails{$email} = "$info"; print "<center><b>Your information was added to ou +r system!</b></center>\n"; } else { print "Email was missing"; exit; } } else { print "Name was missing"; exit; } }

Replies are listed 'Best First'.
Re: Form param checking
by Ovid (Cardinal) on Dec 29, 2003 at 22:27 UTC

    Check out Data::FormValidator. The docs might seem a bit daunting, but it should do the trick for you.

    Update: As for your actual code snippet, I don't see how your $name, $email, and %emails variables are getting populated. Show us how you assign data to them and we might be able to answer your question more directly.

    Cheers,
    Ovid

    New address of my CGI Course.

      This is a snippet:
      if(param()){ my $email = param('email'); my $name = param('name'); my $update = param('update'); my $add1 = param('add1'); my $add2 = param('add2'); my $city = param('city'); my $zip = param('zip'); my $country = param('country'); if($name) { if($email) { if(exists $emails{$email}) { print "<center><b>Email already exists in database +.</b></center>\n"; } else { my $info = join("::", $name, $add1, +$add2, $city, $zip, $country); $emails{$email} = "$info"; print "<center><b>Your information was added to ou +r system!</b></center>\n"; } else { print "Email was missing"; exit; } } }
Re: Form param checking
by PodMaster (Abbot) on Dec 29, 2003 at 22:31 UTC
Re: Form param checking
by Roger (Parson) on Dec 29, 2003 at 22:48 UTC
    use strict; use warnings; use CGI; my $cgi = new CGI; my $name = $cgi->param('name'); my $email = $cgi->param('email'); %emails = ... # fetched from database I assume if (defined $name && defined $email) { if (exists $emails{$email}) { # email exists in database already } else { # add email to system } } else { if (defined $name) { # email is missing } elsif (defined $email) { # name is missing } else { # everything is missing } }
Re: Form param checking
by neuroball (Pilgrim) on Dec 29, 2003 at 22:59 UTC
    You might want to use CGI.pm as it was intended. The following example is taken from "CGI Programming with Perl, Second Edition":
    #!/usr/bin/perl -wT use strict; use CGI; my $q = new CGI; print $q->header( "text/plain" ); print "These are the parameters I received:\n\n"; my( $name, $value ); foreach $name ( $q->param ) { print "$name:\n"; foreach $value ( $q->param( $name ) ) { print " $value\n"; } }
    Which we would change as following to fit your example:
    use CGI; my $q = new CGI; print $q->header( "text/html" ); if ( $q->param() ) { if( $q->param( $name ) ) { if( $q->param( $email ) ) { if( exists $emails{$q->param( $email )} ) { print "<center><b>Email already exists in database.</b +></center>\n"; } else { $emails{$q->param( $email )} = "$info"; print "<center><b>Your information was added to our sy +stem!</b></center>\n"; } } # you missed this closing brace else { print "Email was missing"; exit; } } else { print "Name was missing"; exit; } }

    And last but not least, you might want to write your code so that you can see easily when you are missing an opening/closing code block brace.

    /oliver/

    PS: You might want to check the information you add to the emails hash for validity.

      I'm curious what you mean by
      You might want to use CGI.pm as it was intended.

      The only difference I see (besides fixing the missing brace) is using the object-oriented style rather than the functional style. Did I miss something?

        Well, if the only difference is using the OO style over the functional style, then i say use the functional style. Hands Down. The only time i use the OO style is when i absolutely must ... but until those moments, i use the functional style because it looks cleaner and requires less typing.

        I think neuroball was being a bit subjective by claiming that is how CGI.pm was intended to be used. Especially upon viewing this snippet of unchanged code:

        print "<center><b>Email already exists in database.</b></center>\n";
        which i would have changed to:
        print p({align=>'center'}, b('Email already exists in database'));
        As long as you use param(), header(), and upload(), (correctly) you are using CGI.pm correctly, in my humble opinion. CGI.pm surely subscribes to Perl's philosophy of There Is More Than One Way To Do It!

        I think the outline that Roger provided you with is an excellent place to start studying. You should always write out some kind of design of the possible scenarios ... be it pseudo-code or geometrical shapes with arrows ... before you just start hacking out some code. How can you solve the problem if you don't even understand what the problem is?

        But, do heed Ovid and Podmaster's suggestion to use the CPAN! The idea is that someone has already solved this problem for you, the catch is that you still have to understand what the problem is. That and having a inkling of howto RTFM. ;)

        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)