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

I'm having problems in the area of my if then elsif statement. Need to inject a if then elsif statement so if any of my fields are blank within my HTML form to then send a error mesg back to user until all fields have been inputted then I will send a confirmation mesg....Can anyone show me how to correctly adjust this section
#!/usr/bin/perl -w # $form_data = $ENV{'QUERY_STRING'}; $form_data =~ s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C",hex ($1))/eg; # Replace "+" sign with " "space char. $form_data =~ s/\+/ /g; # Split $form_data into name/value pairs: @fields = split (/&/, $form_data); # Init variables with form data values from following fields: # From Text Fields: ($form_name, $candidate) = split (/=/, $fields[0]); ($form_name, $position) = split (/=/, $fields[1]); # From Radio buttons: ($form_name, $education) = split (/=/, $fields[2]); ($form_name, $status) = split (/=/, $fields[3]); ($form_name, $hired) = split (/=/, $fields[4]); # From Comments Field: ($form_name, $comments) = split (/=/, $fields[5]); # Print and append file dataform.html # Heres the area I'm having problems with... # Need to inject a if then elsif statement because # if any of my fields are blank within my HTML form I need it to send # a error mesg until all fields have been inputted then I need to send # the below confirmation mesg....Can anyone show me how to correctly a +djust # my script:) if (!$.....){ print "error }elsif (!$.....){ print error.. ... .. } esle # print good stuff to screen and to { open(OUT, ">> dataform.html") or die "Can't open file: $!"; print OUT "CANDIDATE: $candidate\n"; print OUT "POSITION TYPE: $position\n"; print OUT "LEVEL OF EDUCATION: $education\n"; #print OUT "STATUS OF AGREEMENT: $status\n"; #print OUT "WAS HE OR SHE HIRED?: $hired\n"; close OUT; } # Send back user confirmation: print << "END_OF_REPLY"; Content-type: text/html <HTML> <HEAD> <TITLE> Confirmation</TITLE> </HEAD> <BR><BR><BR> <H1 ALIGN=CENTER>Thankyou $candidate !</H1> <BR> <H1 ALIGN=CENTER>The $position position is vacant!!</H1> <BR> <H1 ALIGN=CENTER>It will take us several weeks to process.</H1> <BR> <H1 ALIGN=CENTER>Computer Network Division Inc</H1> <BR> </HTML> END_OF_REPLY

Edit: chipmunk 2001-11-30

Replies are listed 'Best First'.
Re: Can anyone adjust my script correctly:)
by maverick (Curate) on Nov 30, 2001 at 22:44 UTC
    first off
    use strict; use CGI;
    The first will help you catch a lot of prgramming errors, and the second will handle all the CGI interaction.

    Aside from that....instead of using an if elsif construct, use a foreach over a set of required keys. With the if/else you'd only be able to report 1 error, when you'd really want to report them all. Try something like this.

    my $query = new CGI; my %FORM = $query->Vars; my @required_fields = qw{candidate position education status hired}; my @errors; foreach (@required_fields) { if (!defined($FORM{$_})) { push(@errors,$_); } } if (@errors) { foreach (@errors) { print "$_ is a required field\n"; } } else { # everything is good, do the work }
    HTH

    /\/\averick
    perl -l -e "eval pack('h*','072796e6470272f2c5f2c5166756279636b672');"

Re: Can anyone adjust my script correctly:)
by Masem (Monsignor) on Nov 30, 2001 at 22:45 UTC
    First off, you really should not be reinventing the wheel; CGI.pm does all this for you and has been put through the security wringer more than enough times to be proven safe.

    Secondly, assuming you stay with this solution is that you are assuming that the form elements will be returned in the QUERY in a specific order. I can't find anything in either the CGI or the HTTPD specifications on order, and while I'd suspect most browser return these fields in the order they were defined in the HTML document, this is not a guarentee. I would verify your form name before arbitarily assigning it to what you believe it is.

    To simply check for empty fields, just use $candidate eq ''.

    -----------------------------------------------------
    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
    "I can see my house from here!"
    It's not what you know, but knowing how to find it if you don't know that's important

Re: Can anyone adjust my script correctly:)
by orkysoft (Friar) on Dec 01, 2001 at 03:25 UTC

    Looks like you could use this form to try and hire a CGI programmer! :-)

    Seriously, follow the other monks' advice, use CGI;, and don't type esle.

(ichimunki)Re: Can someone help me adjust the below if then elsif statement:
by ichimunki (Priest) on Nov 30, 2001 at 23:05 UTC
    Your life may be easier if you do the following before going too much further:

    1) use strict; at the top of your script.

    2) use taint; on all CGI scripts-- not later after you get the logic built, but now when it's easy.

    3) use CGI.pm; on all CGI scripts involving anything remotely resembling a form. There are approximately ten perl hackers worldwide who could write and understand a decent form handler in an efficient amount of time, and all of them would use CGI.pm unless they were in the process of rewriting CGI.pm.

    4) There doesn't appear to be anything wrong with your if-else structure (although I'd probably use a loop of some sort if it were my script) except typos, but I sense that the code you've listed is not the actual code-- which makes it hard to spot the actual error or trouble you're having.

    5) Note that once you use CGI.pm to both create and handle the submission form, you will get a stateful treatment of all the form elements so that if you test and find that one or more params are empty, you simply resend the form (perhaps with a note at the top that says, "error! you've missed the X and Y and Z fields.") and CGI.pm will fill in any values that have already been submitted.

    For more information on using CGI effectively I strongly recommend the Tutorials on Outside Links. The perldocs for CGI are also pretty good (enough to get started on simple forms, for sure) and those are already available on any computer with Perl installed (as is the module).