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

When my form loads it prints "Please enter your name". That happens before I even press the submit button. And when I press the button, it doesn't do anything because it thinks the name is missing.

Can someone see why?

my $name = param('name'); my $email = param('email'); my $url = param('url'); my $message = param('message'); my $reply = param('reply'); print header, start_html('Contact the webmaster'); if (param('Submit')) { if($name) { if($email) { if($message) { open (MAIL, "|$sendmail -t") or die "Cannot access mai +l"; print MAIL "To: $adminmail\n"; print MAIL "From: $email\n"; print MAIL "Subject: $subject\n\n"; print MAIL "Who: $name\n"; print MAIL "Where: $email\n"; print MAIL "Message: $message\n"; print MAIL "Reply: $reply\n"; close(MAIL) or die "Cannot close sendmail $!"; print <<"ALL"; <table width="369" border="0"> <tr> <td colspan="2">You sent the following message: </td> </tr> <tr bgcolor="#CCCCCC"> <td width="75">Name:</td> <td width="278">$name</td> </tr> <tr bgcolor="#CCCCCC"> <td>Email:</td> <td>$email</td> </tr> <tr bgcolor="#CCCCCC"> <td>URL:</td> <td>$url</td> </tr> <tr bgcolor="#CCCCCC"> <td>Message:</td> <td>$message</td> </tr> <tr bgcolor="#CCCCCC"> <td>Reply:</td> ALL if($reply) { print "<td>yes</td>"; } else { print "<td></td>"; } print "</tr></table>"; exit; } else { print "<font color=red>Please fill in a message.</font>\n"; }} } else { print "<font color=red>Please fill in your email address.</fon +t>\n"; } } else { print "<font color=red>Please fill in your name.</font>\n"; } print <<"ALL"; <table width="469" border="1" align="center"><form method="POST" actio +n=""> <tr> <td><p>Thank you for visiting my site. If for any reason you want +to contact me, whether it be for site bugs, custom work or just a &qu +ot;Hey, you!&quot;, please feel free to contact me. </p> <p>If you request a reply, expect it within the next 12-24 hours. +</p></td> </tr> <tr> <td bgcolor="#CCCCCC">Name: </td> </tr> <tr> <td><input name="name" type="text" id="name"></td> </tr> <tr> <td bgcolor="#CCCCCC">Email:</td> </tr> <tr> <td><input name="email" type="text" id="email"></td> </tr> <tr> <td bgcolor="#CCCCCC">URL:</td> </tr> <tr> <td><input name="url" type="text" id="url"></td> </tr> <tr> <td bgcolor="#CCCCCC">Message:</td> </tr> <tr> <td><textarea name="message" cols="40" rows="5" id="message"></tex +tarea></td> </tr> <tr> <td><input name="reply" type="checkbox" id="reply" value="checkbox +"> Do you want a reply? (checked = yes) </td> </tr> <tr> <td bgcolor="#CCCCCC"><div align="center"> <input type="submit" name="Submit" value="Submit"> </div></td> </tr> </form></table> ALL


"Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

sulfericacid

Replies are listed 'Best First'.
Re: Form problems
by dws (Chancellor) on Jan 10, 2004 at 19:29 UTC
    Can someone see why?

    When facing code like this, I often find that the problem pops right out when I do a little refactoring. I'd start by breaking out two separate subroutines, along the lines of:

    if ( param('Submit') ) { handleSubmit(); } else { emitForm(); }
    Right away, this breaks the problem down into smaller pieces. Then, I'd rewire logic in handleSubmit to handle the error cases first as "guard clauses". Something like:
    if ( $name eq "" ) { print "<font color=red>Blah blah blah.</font>"; return; } if ( $email eq "" ) { print "<font color=red>Blah blah blah.</font>"; return; } ... open(MAIL, "|$sendmail -t") or die "Cannot access mail"; print MAIL "To: $adminmail\n"; print MAIL "From: $email\n"; ...
    At this point, I'd have an "oh shit" moment, and would arrange to turn on taint-checking and add code to validate $email to ensure that someone isn't sneaking in an exploit, but that's really a separate issue.

    Try this approach, and see if you can't figure the problem out yourself.

Re: Form problems
by b10m (Vicar) on Jan 10, 2004 at 19:21 UTC

    Classic curly problem :) Change this:

    { print "<font color=red>Please fill in a message.</font>\n"; }} # ^^ # Error is here

    into:

    { print "<font color=red>Please fill in your name.</font>\n"; } } # Move one of the two curlies all the way down here
    --
    b10m
Re: Form problems
by TomDLux (Vicar) on Jan 11, 2004 at 03:40 UTC

    What dws said.

    I would phrase the conditions a little differently, myself. I read his conditions as, "If $email is identical to the empty string", while my brain says, "if $email is empty":

    if ( 0 == length $email ) {

    Of course, it's all a matter of preference. I don't imagine there's much optimization differencee between string comparison to an empty string, and comparing zero to the length of an empty string.

    --
    TTTATCGGTCGTTATATAGATGTTTGCA