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

I am trying to create a simple that takes user input of their e-mail address or fax number, and sends it to me in an e-mail message. I'd like the form to require that 1 of these fields be provided by the user OR an error conditions occurs. Then, the user is taken back to form to input appropriate value. I am VERY NEW to perl, and I am unsure of what to code. Any advise? Much appreciated! Tom Fitzsimmons psutom2@home.com

Replies are listed 'Best First'.
Re: form help
by mexnix (Pilgrim) on Aug 13, 2001 at 09:46 UTC
    You're new to perl so I'll give you the standard rant: look into CGI.pm it should come standard with your perl distro. Check out Ovid's CGI Course (Very good stuff). Also, get your hands on a copy of "Learning Perl" published by O'Reilly and written by merlyn. All great stuff! Welcome tomfitz!

    __________________________________________________
    <moviequote name="Hackers">
    The Plague: [...]Well let me explain the New World Order. Governments and corporations need people like you and me. We are samurai. The keyboard cowboys. And all those other people out there who have no idea what's going on are the cattle. Mooo!
    </moviequote>

    mexnix.perlmonk.org

Re: form help
by tachyon (Chancellor) on Aug 13, 2001 at 10:48 UTC

    Here is the bare bones to get you started. This CGI will display a short form when first called and process the input of the generated form when called by that form. You may find my tutorial at CGI Help Guide useful in getting it working (this is now part of Ovid's excellent CGI tutorial so you should see it there)

    #!/usr/local/bin/perl -wT use strict; # ensure all fatals go to browser during debugging and setup # *don't* uncomment these 5 lines on production code for security #BEGIN { # $|=1; # print "Content-type: text/html\n\n"; # use CGI::Carp('fatalsToBrowser'); #} $|=1; print "Content-type: text/html\n\n"; # print a header # make the environment safer delete @ENV{qw(IFS CDPATH ENV BASH_ENV)}; $ENV{'PATH'} = '/usr/bin:/usr/local/bin'; # CGI.pm stuff use CGI; $CGI::DISABLE_UPLOADS = 1; # disable uploads $CGI::POST_MAX = 1024; # limit post to 1kB my $query = new CGI; # get a CGI object # a few config vars my $mail_prog = '/usr/lib/sendmail'; my $our_email = 'us@us.com'; my $us = "Just another Perl Hacker"; # get our form input from our CGI.pm object my $formid= $query->param('formid') || ''; my $name = $query->param('name') || ''; my $email = $query->param('email') || ''; my $fax = $query->param('fax') || ''; # logic to decide what to do if ($formid eq "myform") { if ($name and ($email or $fax)) { &Send_Mail; &Thanks; } else { &Error("Invalid input, use Back button and try again") } } else { &Show_Form; # first time dispay form } exit; ################# SUBROUTINES ################### sub Send_Mail { open ('MAIL', "|$mail_prog -t") or die "Can't open mail $!\n"; print MAIL <<" TEXT"; To: $our_email Reply-to: $our_email From: $us Subject: Form Input Name: $name Email: $email FAX: $fax TEXT close MAIL; } sub Thanks { print <<" HTML"; <p>Thanks for using my form HTML } sub Error { my $message = shift; print <<" HTML"; <p>$message HTML } sub Show_Form { print <<" HTML"; <html> <head> <title>Form</title> </head> <body> <form method="POST" action="/cgi-bin/mycgi.cgi"> <input type="hidden" name="formid" value="myform"> <table border="0" width="400" cellspacing="0" cellpadding="0"> <tr> <td width="60">Name </td> <td width="340"><input type="text" name="name" size="20"></t +d> </tr> <tr> <td width="60">Email </td> <td width="340"><input type="text" name="email" size="20"></ +td> </tr> <tr> <td width="60">FAX </td> <td width="340"><input type="text" name="fax" size="20"></td +> </tr> <tr> <td width="60"></td> <td width="340"> <input type="submit" value="Submit" name="submit"> <input type="reset" value="Reset" name="reset"> </td> </tr> </table> </form> </body> </html> HTML }

    cheers

    Update

    Fixed path typos in $ENV{'PATH'} line thanks to c

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: form help
by anguz (Novice) on Aug 13, 2001 at 16:43 UTC
    Hmm, I know it's not Perl, but in my opinion form validation on the web is faster and easier to do in simple cases like this using Javascript.

    Then you don't have to process the form on the server just to see if the user filled in one field OR another. The web browser does this and can display a warning message on the screen as well without any traffic to the server being generated.

    Simple functionality like this should be fairly browser-independent. If you are targetting browsers that don't support Javascript, though, use Perl;

      JavaScript would be good to keep the user from hitting Submit with a bad email address or phone number, but you really should be checking to make sure that it's valid data if they have JavaScript off, or are intentionally bypassing your form to try and break your server, right? Besides, it's fun coming up with messages to tell the script kiddies you're onto their game. :) IMHO, using both, with JavaScript as a first line of idiotproofing, doesn't really hurt.

      andre germain
      "Wherever you go, there you are."

        I agree !

        Simple form validation on the front to help users to enter the correct data (and make sure required fields are filled in). Perl logic at the back to make sure the whole lot is solid and won't break anything.

Re: form help
by X-Factor (Initiate) on Aug 13, 2001 at 18:07 UTC
    use something like this....
    #!/usr/bin/perl require "/web/cgi-bin/cgi-lib.pl"; &ReadParse; ### get values from html form input called email & fax $fax = $in{'fax'}; $email = $in{'email'}; $err[1] = "Please fill in all the required fields"; $domain = "your.domain.com"; ########## did they enter something? &error($err[1]) if !( $fax || $email ); &mail exit; sub error { print "Content-type: text/html\n\n"; print "<HTML><HEAD></HEAD><BODY>\n"; print "<H3>$_[0]</H3><BR>\n"; print "</BODY></HTML>\n"; exit 0; } ########## use sendmail sub mail { open (MAIL,"|/usr/lib/sendmail Me\@$domain"); print MAIL "From: script@domain.com\n"; print MAIL "To: Me\@$domain\n\n"; print MAIL "Users Fax:$fax\n"; print MAIL "Users Email:$email\n"; close MAIL; return; }
      "cgi-lib.pl" is as dead as Perl 4. CGI is a much more robust solution that takes advantages of Perl 5 features (such as references). I would instead write something like:
      #!/usr/bin/perl -wT use CGI; my $q = CGI->new(); my $fax = $q->param('fax'); my $email = $q->param('email'); print $q->header(); error('Please fill in all the required blanks') unless ($fax && $email); mail(); # print success method, redirect? sub error { print <<HTML; <HTML><HEAD></HEAD><BODY> <H3>$_[0]</H3><BR> </BODY></HTML> HTML exit 1; }
      I'd also use Mail::Send or Mail::Mailer instead of opening a pipe directly to sendmail.