469,332 Members | 6,678 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,332 developers. It's quick & easy.

Perl - Form handler

Dear all,

It been more than 3 days I am trying to debug this program, I
interpret it using activePerl and it is giving (perl -wc
code_process.pl) no error syntax but when I put it online, change to
the appropriate mode and test the html form it gave me an 500 internel
server error.

I really need it to be done soon, I would not post here before I test
it as much as I can,

In case you would like to check the html too, please let me know.

regards,
John

#!/usr/local/bin/perl

$forward_url_eng = 'www.anything.com/eng/formsent.html';
$forward_url_fra = 'www.anything.com/fra/formsentf.html';

$htmlbody="<body bgcolor=\"#F2F6E9\" text=\"#000000\" link=\"#8B2833\"
vlink=\"#000000\" alink=\"#FF0000\" leftmargin=\"0\" topmargin=\"0\"
rightmargin=\"0\" bottommargin=\"0\" marginwidth=\"0\"
marginheight=\"0\">";

# Parse Form Contents
&parse_form;

# Generate ID
&generate_id;

# Return HTML Page
&return_html;

# Send E-Mail
&send_mail;

#================================ Parse Form
#=====================================

sub parse_form {

# Get the input
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
# Split the name-value pairs
@pairs = split(/&/, $buffer);

foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$name =~ tr/+/ /;
$name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ s/<!--(.|\n)*-->//g;

if ($name eq 'required') {@required = split(/,/,$value);}
elsif ($name eq 'env_report') {@env_report = split(/,/,$value);}
else {
if ($FORM{$name} && ($value)) {$FORM{$name} = "$FORM{$name},
$value";
}
elsif ($value) {$FORM{$name} = $value;
}
}
}
}

#================================ Generate ID
=====================================

sub generate_id
{
$lower=100;
$upper=999;
$random = int(rand( $upper-$lower+1 ) ) + $lower;
@f = (localtime)[3..5]; # grabs day/month/year values
$DD = ($f[1] + 1);
if ($DD < 10) {$DD = 0 . $DD};
$MM = $f[0];
if ($MM < 10) {$MM = 0 . $MM};
$ID = $DD . "-" . $MM . "-" . substr(($f[2] + 1900),2,2) . "-" .
$random;
}

#================================ Return HTML
=====================================

sub return_html {
print "Content-type: text/html\n\n";
print "<HTML>";
print "<HEAD>";
if ($FORM{'formlang'} eq 'English') {print "<META
HTTP-EQUIV=\"Refresh\"
CONTENT=\"0; URL=$forward_url_eng\">\n";}
elsif ($FORM{'formlang'} eq 'French') {print "<META
HTTP-EQUIV=\"Refresh\"
CONTENT=\"0; URL=$forward_url_fra\">\n";}

print "</HEAD>";
print "$htmlbody\n ";
print "</body>\n</html>";
print "</HTML>";
}
#======================================== Send mail
#===============================================

sub send_mail {

# ==== Next three lines for Unix
$mail_path = "/usr/sbin";
$mail_prog = "sendmail";
$mail_temp_path = "/tmp";
# ====== Send mail to ILS ======

local($mail_file);
do {$mail_file=int(rand(99999999)) . ".txt";} until !(-e
$mail_file);

$mail_command = "$mail_path/$mail_prog -F \"$FORM{'email'}\"
$FORM{'recipient'}
< $mail_temp_path/$mail_file";
open (MAIL,">$mail_temp_path/$mail_file") || die "CANT OPEN
$mail_temp_path/$mail_file\n";

$mail_command = "$mail_path$mail_prog -CGI -s\"Application Form
($ID)\"
-f$FORM{'email'} < $mail_temp_path\\$mail_file";
open (MAIL,">$mail_temp_path/$mail_file") || die "CANT OPEN
$mail_temp_path/$mail_file\n";

&mail_to_ILS; # Compose mail to be sent to ILS

close (MAIL);
system ("$mail_command");
unlink($mail_file);

#====== Send mail to Applicant ======

local($mail_file);
do {$mail_file=int(rand(99999999)) . ".txt";} until !(-e
$mail_file);
open (MAIL,">$mail_temp_path/$mail_file") || die "CANT OPEN
$mail_temp_path/$mail_file\n";
if ($FORM{'formlang'} eq 'English')
{
$mail_command = "$mail_path/$mail_prog -F \"$FORM{'recipient'}\"
$FORM{'email'}
< $mail_temp_path/$mail_file";
# $mail_command = "$mail_path$mail_prog -CGI -s\"
Application Form\" -f$FORM{'recipient'} $FORM{'email'} <
$mail_temp_path\\$mail_file";
&mail_to_applicant_eng; # Compose mail to be sent to applicant
(English)
}

elsif ($FORM{'formlang'} eq 'French')
{
$mail_command = "$mail_path/$mail_prog -F \"$FORM{'recipient'}\"
$FORM{'email'}
< $mail_temp_path/$mail_file";
# $mail_command = "$mail_path$mail_prog -CGI -s\"Inscription \"
-f$FORM{'recipient'} $FORM{'email'} < $mail_temp_path\\$mail_file";
&mail_to_applicant_fra; # Compose mail to be sent to applicant
(French)
}

close (MAIL);
system ("$mail_command");
unlink($mail_file);
}

#======================================== Mail to ILS
#===============================================

sub mail_to_ILS {
print MAIL "Reply-To: $FORM{'email'}\n";
print MAIL "Subject: Application Form ($ID)\n";
print MAIL "From: $FORM{'email'}\n";
print MAIL "Application Form, ID: ($ID)\n";
print MAIL "--------------------------------------------\n";
print MAIL "Language: $FORM{'formlang'}\n";
print MAIL "--------------------------------------------------------------\n";
print MAIL "1. Personal Information:\n";
print MAIL " Family name(s): $FORM{'lname'}\n";
print MAIL " Given name(s): $FORM{'fname'}\n";
print MAIL " Employee Number: $FORM{'num_employe'}\n";
print MAIL " Other Contact Information:\n";
if ($FORM{'phonehome'} ne '') {print MAIL " Telephone numbers
(Home):
$FORM{'phonehome'}\n";};
if ($FORM{'phonework'} ne '') {print MAIL " Telephone numbers
(Work):
$FORM{'phonework'}\n";};
print MAIL " E-mail: $FORM{'email'}\n";
print MAIL "--------------------------------------------------------------\n";
print MAIL "2. I am a member of:\n";
print MAIL " The faculty of : $FORM{'faculte'}\n";
if ($FORM{'faculte'} ne '') {print MAIL " $FORM{'faculte'}\n";};
print MAIL " Departement: $FORM{'departement'}\n";
if ($FORM{'departement'} ne '') {print MAIL "
$FORM{'departement'}\n";};
print MAIL " Unit: $FORM{'unite'}\n";
if ($FORM{'unit'} ne '') {print MAIL " $FORM{'unite'}\n";};
print MAIL " I am a : $FORM{'job'}\n";
print MAIL " My status is : $FORM{'status'}\n";
#if ($FORM{'statut-autre'} ne '') {print MAIL " Year:
$FORM{'statut-autre'}\n";};
print MAIL "--------------------------------------------------------------\n";
print MAIL "3. I wish to enroll in the following course(s) and
session(s):\n";

if ($FORM{'a1'} ne '') {print MAIL " $FORM{'a1'}\n";};
if ($FORM{'a2'} ne '') {print MAIL " $FORM{'a2'}\n";};
if ($FORM{'a3'} ne '') {print MAIL " $FORM{'a3'}\n";};
if ($FORM{'a4'} ne '') {print MAIL " $FORM{'a4'}\n";};
if ($FORM{'a5'} ne '') {print MAIL " $FORM{'a5'}\n";};
if ($FORM{'a6'} ne '') {print MAIL " $FORM{'a6'}\n";};

if ($FORM{'h1'} ne '') {print MAIL " $FORM{'h1'}\n";};
if ($FORM{'h2'} ne '') {print MAIL " $FORM{'h2'}\n";};
if ($FORM{'h3'} ne '') {print MAIL " $FORM{'h3'}\n";};
if ($FORM{'h4'} ne '') {print MAIL " $FORM{'h4'}\n";};
if ($FORM{'h5'} ne '') {print MAIL " $FORM{'h5'}\n";};

if ($FORM{'i1'} ne '') {print MAIL " $FORM{'i1'}\n";};
if ($FORM{'i2'} ne '') {print MAIL " $FORM{'i2'}\n";};
if ($FORM{'i3'} ne '') {print MAIL " $FORM{'i3'}\n";};

print MAIL "--------------------------------------------------------------\n";
print MAIL "4. How do you intend to be classified?\n";
print MAIL " Answer: $FORM{'test'}\n";
#if ($FORM{'date_test'} ne '') {print MAIL " Year:
$FORM{'date_test'}\n";};
#if ($FORM{'resultat_test'} ne '') {print MAIL " Year:
$FORM{'resultat_test'}\n";};
print MAIL "--------------------------------------------------------------\n";
}

#======================================== Mail to Applicant (English)
#===============================================
sub mail_to_applicant_eng {
print MAIL "Reply-To: $FORM{'recipient'}\n";
print MAIL "Subject: Application Form\n";
print MAIL "From: $FORM{'recipient'}\n";
print MAIL "To: $FORM{'email'}\n";
print MAIL "Dear $FORM{'fname'} $FORM{'lname'},\n\n";
print MAIL "Thank you for choosing the
Institute.\n\n";
print MAIL "Your reference number is \"$ID\". Please use this number
when you write
to us ";
print MAIL "You indicated your intention to enroll in the following
course(s) and session(s):\n";

if ($FORM{'a1'} ne '') {print MAIL " $FORM{'a1'}\n";};
if ($FORM{'a2'} ne '') {print MAIL " $FORM{'a2'}\n";};
if ($FORM{'a3'} ne '') {print MAIL " $FORM{'a3'}\n";};
if ($FORM{'a4'} ne '') {print MAIL " $FORM{'a4'}\n";};
if ($FORM{'a5'} ne '') {print MAIL " $FORM{'a5'}\n";};
if ($FORM{'a6'} ne '') {print MAIL " $FORM{'a6'}\n";};

if ($FORM{'h1'} ne '') {print MAIL " $FORM{'h1'}\n";};
if ($FORM{'h2'} ne '') {print MAIL " $FORM{'h2'}\n";};
if ($FORM{'h3'} ne '') {print MAIL " $FORM{'h3'}\n";};
if ($FORM{'h4'} ne '') {print MAIL " $FORM{'h4'}\n";};
if ($FORM{'h5'} ne '') {print MAIL " $FORM{'h5'}\n";};

if ($FORM{'i1'} ne '') {print MAIL " $FORM{'i1'}\n";};
if ($FORM{'i2'} ne '') {print MAIL " $FORM{'i2'}\n";};
if ($FORM{'i3'} ne '') {print MAIL " $FORM{'i3'}\n";};

print MAIL "We look forward to your arrival and wish you an enjoyable
and rewarding
experience .\n\n";
print MAIL "We invite you to read our Privacy Policy at: ";

print MAIL "We would be pleased to answer any questions you
have.\n\n";

}

#======================================== Mail to Applicant (French)
#===============================================
sub mail_to_applicant_fra {
print MAIL "Reply-To: $FORM{'recipient'}\n";
print MAIL "Subject: Inscription\n";
print MAIL "From: $FORM{'recipient'}\n";
print MAIL "To: $FORM{'email'}\n";
print MAIL "Bonjour $FORM{'fname'} $FORM{'lname'},\n\n";
print MAIL "Nous vous remercions d'avoir choisi
..\n\n";
print MAIL "Le numéro de référence qui vous a été attribué est le
\"$ID\". Nous vous
serions reconnaissants de ";
print MAIL "bien vouloir rappeler ce numéro sur toute
correspondance.\n\n";
print MAIL "Vous avez indiqué votre intention de poursuivre dans les
cours suivants:\n";

if ($FORM{'a1'} ne '') {print MAIL " $FORM{'a1'}\n";};
if ($FORM{'a2'} ne '') {print MAIL " $FORM{'a2'}\n";};
if ($FORM{'a3'} ne '') {print MAIL " $FORM{'a3'}\n";};
if ($FORM{'a4'} ne '') {print MAIL " $FORM{'a4'}\n";};
if ($FORM{'a5'} ne '') {print MAIL " $FORM{'a5'}\n";};
if ($FORM{'a6'} ne '') {print MAIL " $FORM{'a6'}\n";};

if ($FORM{'h1'} ne '') {print MAIL " $FORM{'h1'}\n";};
if ($FORM{'h2'} ne '') {print MAIL " $FORM{'h2'}\n";};
if ($FORM{'h3'} ne '') {print MAIL " $FORM{'h3'}\n";};
if ($FORM{'h4'} ne '') {print MAIL " $FORM{'h4'}\n";};
if ($FORM{'h5'} ne '') {print MAIL " $FORM{'h5'}\n";};

if ($FORM{'i1'} ne '') {print MAIL " $FORM{'i1'}\n";};
if ($FORM{'i2'} ne '') {print MAIL " $FORM{'i2'}\n";};
if ($FORM{'i3'} ne '') {print MAIL " $FORM{'i3'}\n";};

print MAIL "Nous nous réjouissons de bientôt pouvoir vous accueillir
et nous
espérons que votre séjour parmi nous ";
print MAIL "sera à la fois une expérience agréable et
enrichissante.\n\n";
print MAIL "Nous vous invitons à prendre connaissance de notre
Politique de
protection de la confidentialité à : ";
print MAIL "merci\n\n";
print MAIL "Nous nous ferons un plaisir de répondre à toute question
que vous
pourriez vouloir nous poser.\n\n";
print MAIL "Nous vous prions d'agréer nos salutations
distinguées.\n\n";

}
Jul 19 '05 #1
3 5394
John wrote:
It been more than 3 days I am trying to debug this program, I
interpret it using activePerl and it is giving (perl -wc
code_process.pl) no error syntax but when I put it online, change
to the appropriate mode and test the html form it gave me an 500
internel server error.
What does the server's error log tell you? If you can't access it, you
can add this line in the beginning of the script so as to have fatal
errors printed to the screen:

use CGI::Carp 'fatalsToBrowser';
In case you would like to check the html too, please let me know.


To be honest, I don't want to dig into the details in that program,
since it's old and not very good according to today's standards.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Jul 19 '05 #2
In article <46**************************@posting.google.com >, John
<jo*******@hotmail.com> wrote:
Dear all,

It been more than 3 days I am trying to debug this program, I
interpret it using activePerl and it is giving (perl -wc
code_process.pl) no error syntax but when I put it online, change to
the appropriate mode and test the html form it gave me an 500 internel
server error.

I really need it to be done soon, I would not post here before I test
it as much as I can,

In case you would like to check the html too, please let me know.


I can give you the standard advice for these types of problems:

1. Run the CGI on the command-line (but you have already done this).
2. Look at the server error logs; what do they say?
3. Use the CGI module; don't try to parse form input yourself.
4. Put "use CGI::Carp 'fatalsToBrowser';" in your program (after doing
3)
5. Minimize your program to attempt to isolate the problem and post
that minimal program, what it is doing, what the server logs say, if
anything, and what you expect it to be doing that it is not.

FYI: this newsgroup is defunct; try comp.lang.perl.misc in the future.
Jul 19 '05 #3
thank you Gunnar Hjalmarsson, I will keep searching to solve the bug.

regards,
John.
Gunnar Hjalmarsson <no*****@gunnar.cc> wrote in message news:<Qb******************@newsb.telia.net>...
John wrote:
It been more than 3 days I am trying to debug this program, I
interpret it using activePerl and it is giving (perl -wc
code_process.pl) no error syntax but when I put it online, change
to the appropriate mode and test the html form it gave me an 500
internel server error.


What does the server's error log tell you? If you can't access it, you
can add this line in the beginning of the script so as to have fatal
errors printed to the screen:

use CGI::Carp 'fatalsToBrowser';
In case you would like to check the html too, please let me know.


To be honest, I don't want to dig into the details in that program,
since it's old and not very good according to today's standards.

Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by John C. Worsley | last post: by
13 posts views Thread by Jesse Thompson | last post: by
6 posts views Thread by Richard Trahan | last post: by
3 posts views Thread by amerar | last post: by
reply views Thread by crawfordr | last post: by
5 posts views Thread by sasimca007 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by haryvincent176 | last post: by
reply views Thread by Marylou17 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.