You are here:

Perl & CGI/PERL form script

Advertisement


Question
Hi I am new to PERL/cgi scripting. I have just recently taken over a website for a client and inherited a registration form with it. I am having a difficult time getting the form to execute properly. It will randomly work here and there but usually the 'internal server error 500' pops up after the submit button is used. I have been testing and reading and have even talked to the originator of the form, who did get it working but then without any editing by me, the error comes back. I am stumped right now. I really need to get this working and or start over (hopefully not).

Any help would be great!

The web page is here.
http://www.growningrey.org/register.html

THE CGI SCRIPT IS BELOW:
#!/usr/bin/perl
# A general purpose form decoder for use with all forms.
#
# what it does is accept any information from ANY HTML form
# formats it, and mails it.
#
# To operate properly, the form must have the following
# entries:
# <INPUT TYPE="hidden" NAME="FORM_NAME" VALUE="The Name of your form">        
# <INPUT TYPE="hidden" NAME="MAIL_TO" VALUE="">
# This way the person receiving the mail knows where it came from
# The MAIL_TO is the person you want to receive the submission
#
# 1997 by bruce gronich for the public domain.
#This program is intended to be used as a learning tool. It
#is given freely AS IS and comes with no warranty. The author
#is not responsible for any damages caused by its use or
#misuse.

#cp the funform.html to your html root directory.
#modify the funform.html to meet your needs.
#change the name of the path of the /cgi-bin/ if needed

#BNB SAYS- Configure this to your sendmail
$mail_program="/usr/lib/sendmail -t ";

#BNB SAYS- Set these options to meet your needs

#let SEND_MAIL="Y" when you are done testing and want mail to be sent
$SEND_MAIL="Y";

######################################################################
###### THIS IS WHERE OUR PROGRAM STARTS! #############################
######################################################################

#This first part of the program splits up our data and gets it
#ready for formatting and mailing.
 $i=0;
 read(STDIN,$temp,$ENV{'CONTENT_LENGTH'});
 @pairs=split(/&/,$temp);
 foreach $item(@pairs)
  {
   ($key,$content)=split(/=/,$item,2);
   $content=~tr/+/ /;
   $content=~s/%(..)/pack("c",hex($1))/ge;
   $fields{$key}=$content;
   $i++;
   $item{$i}=$key;
   $response{$i}=$content;
  }

#Re-assign a couple of variables for readability reasons
   $send_to = ;
   $form_id = $fields{Form};
   $msg_from = $fields{E_mail};
  $status = $fields{resubmit};
  $respond = $fields{response};
  $school = $fields{schoolname};

#additional addresses to send form to - be sure to include comma separators between entries and before first one!
  $also_send_to = ,;

#Set up our HTML Result Form
  print "Content-type: text/html\n\n";
  print "<HTML>\n";
  print "<HEAD>\n";
  print "<TITLE>Thank you for registering</TITLE>\n";
  print "<LINK REL=STYLESHEET TYPE=TEXT/CSS HREF=http://www.growningrey.org/cgi.css>\n";
  print "</HEAD>\n";
  print "<BODY BGCOLOR=#FFFFFF>\n;

  if ($SEND_MAIL eq "Y")
    {
      open (MAIL, "|$mail_program") ||
         die "Unable to run mail program\n";

print MAIL <<__STOP_OF_HEADER__;
To: $send_to$also_send_to
Reply: $eemail
Subject: $form_id $status Form submission from $school

__STOP_OF_HEADER__
    }

  print "<table bgcolor=green cellpadding=0 cellspacing=0 width=100%>\n";
  print "<td width=107>\n";
  print "<img src=http://www.growningrey.org/logo.gif>\n";
  print "</td>\n";
  print "<td>\n";
  print "<strong>\n";
  print "<big>\n";
  print "<big>\n";
  print "<center>\n";
  print "<font face=arial color=#ffffff>Grown in Grey- Registration Response</font>\n";
  print "</center>\n";
  print "</big>\n";
  print "</big>\n";
  print "</strong>\n";
  print "</td>\n";
  print "<td width=107>\n";
  print "<img src=http://www.growningrey.org/logo.gif>\n";
  print "</td>\n";
  print "</table>\n";
  print "<p>\n";
  print "<font class=thanks>Thank you for registering.</font>\n";
  print "</p>\n";
  print "<p class=text>Here is the information you sent.<br>";
  print "You can print this page or save it to your hard drive if you wish.<br>\n";
  print "We will confirm your registration by $respond.</p>\n";
  print "
";
#start at fourth item - don't need to include first two in responses 
  
   $i=4;
     while ( $item{$i} gt " ")
       {
         if ($SEND_MAIL eq "Y")
           {
          print MAIL "      $item{$i};$response{$i}\n";
           }
         print "$item{$i}:   $response{$i}\n";
         $i++;
       }
   print "
";
  print "<p class =link><a href=http://www.growningrey.org/index.html>Click Here to to go back to the Grown in Grey Home Page</a></p>\n";
  print "</BODY>\n";
  print "</HTML>\n";

#CLOSE THE MAIL PROGRAM IF WE ARE MAILING
  if ($SEND_MAIL eq "Y")
     {
        close (MAIL);
     }

######################################################################
###### THIS IS WHERE OUR PROGRAM ENDS! ###############################
######################################################################

Answer
There are a number of mistakes in this script that I see right off the bat, but it may not be enough to get it running.

There are some hash variables referenced without quotes:

$form_id = $fields{Form};
$msg_from = $fields{E_mail};
$status = $fields{resubmit};
$respond = $fields{response};
$school = $fields{schoolname};

Should be:

$form_id = $fields{'Form'};
$msg_from = $fields{'E_mail'};
$status = $fields{'resubmit'};
$respond = $fields{'response'};
$school = $fields{'schoolname'};


Everytime they appear, be sure they're quoted like this.

Also, I noticed some quotes are weird--

 $send_to = ;

this should be:

 $send_to = "";

or

 $send_to = '';

Be sure you upload in ASCII, and set the permissions to 755. Also be sure that the script is in the CGI-BIN, or a folder that is allowed to execute scripts.

Perl & CGI

All Answers


Answers by Expert:


Ask Experts

Volunteer


Jason Silver

Expertise

I can answer questions about script installation problems. I can give hints and tips for writing code. I can suggest the best approaches for writing web applications, and other similar topics.

Experience

I've been writing Perl scripts since 1999. I sell my scripts at www.intelliscript.net

Education/Credentials
I'm self taught in computer programming.

Awards and Honors
I've been quoted in a computer programming publication, called CGI-101.

©2016 About.com. All rights reserved.