-1

可能在我看来一切!

我基本上不知道我在做什么。我以前从未编写过 .cgi 文件,但我确实根据几个教程编写了脚本......其中之一就是这个...... http://www.yourhtmlsource.com/cgi/processingforms.html

我想要一个 Perl 脚本,它将处理一个 html 表单,收集用户的数据并发送到电子邮件,并在用户点击 SUBMIT 时返回Thank you.html 文件给用户。

#!/usr/bin/perl
use CGI::Carp qw(fatalsToBrowser);

if ($ENV{'REQUEST_METHOD'} eq 'POST') {

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

@pairs = split(/&/, $buffer);

foreach $pair (@pairs) {
    ($name, $value) = split(/=/, $pair);
    $value =~ tr/+/ /;
    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
    $FORM{$name} = $value;

}

if( $FORM{magazine} ){
    $magazine_flag ="ON";
}else{
    $magazine_flag ="OFF";
}

if( $FORM{internet} ){
    $internet_flag ="ON";
}else{
    $internet_flag ="OFF";
}

if( $FORM{distributor} ){
    $distributor_flag ="ON";
}else{
    $distributor_flag ="OFF";
}

if( $FORM{yes1} ){
    $yes1_flag ="ON";
}else{
    $yes1_flag ="OFF";
}

if( $FORM{no1} ){
    $no1_flag ="ON";
}else{
    $no1_flag ="OFF";
}

if( $FORM{twofifty} ){
    $twofifty_flag ="ON";
}else{
    $twofifty_flag ="OFF";
}

if( $FORM{fivehundred} ){
    $fivehundred_flag ="ON";
}else{
    $fivehundred_flag ="OFF";
}

if( $FORM{thousand} ){
    $thousand_flag ="ON";
}else{
    $thousand_flag ="OFF";

}

if( $FORM{twofivehundred} ){
    $twofivehundred_flag ="ON";
}else{
    $twofivehundred_flag ="OFF";
}

if( $FORM{fivethousand} ){
    $fivethousand_flag ="ON";
}else{
    $fivethousand_flag ="OFF";

}

if( $FORM{tenthousand} ){
    $tenthousand_flag ="ON";
}else{
    $tenthousand_flag ="OFF";
}

if( $FORM{fifteenthousand} ){
    $fifteenthousand_flag ="ON";
}else{
    $fifteenthousand_flag ="OFF";
}

if( $FORM{yes2} ){
    $yes2_flag ="ON";
}else{
    $yes2_flag ="OFF";
}

if( $FORM{no2} ){
    $no2_flag ="ON";
}else{
    $no2_flag ="OFF";
}

if( $FORM{yes3} ){
    $yes3_flag ="ON";
}else{
    $yes3_flag ="OFF";
}

if( $FORM{no3} ){
    $no3_flag ="ON";
}else{
    $no3_flag ="OFF";
}


$Magazine-name = $FORM{magazine-name};
$Name = $FORM{name};
$Title = $FORM{title};
$Job = $FORM{job};
$Company = $FORM{company};
$Address = $FORM{address};
$City = $FORM{city};
$State = $FORM{state};
$Zip = $FORM{zip};
$Telephone = $FORM{telephone};
$E-mail = $FORM{e-mail};




open (MESSAGE,"| /usr/sbin/sendmail -t");

print MESSAGE "To: aalmeida\@aemc.com\n"; 
print MESSAGE "From: " . $Name . ", reader\n";
print MESSAGE "Reply-to: " . $E-mail . "(" . $Name . ")\n";

print MESSAGE "Subject: NECA 2013 Registration to win from $Name \n\n";

print MESSAGE "$Name wrote:\n\n";
print MESSAGE "Where do you look most often for test instrumentation?:\n\n"; 
print MESSAGE "$magazine_flag\n\n";
print MESSAGE "$magazine-name\n\n";
print MESSAGE "$internet_flag\n\n";
print MESSAGE "$distributor_flag\n\n";
print MESSAGE "$representative_flag\n\n";
print MESSAGE "I have a need for insulation testing:\n\n";
print MESSAGE "$yes1_flag\n\n";
print MESSAGE "$no1_flag\n\n";
print MESSAGE "What test voltage is important?:\n\n";
print MESSAGE "$twofifty_flag\n\n";
print MESSAGE "$fivehundred_flag\n\n";
print MESSAGE "$thousand_flag\n\n";
print MESSAGE "$twofivehundred_flag\n\n";
print MESSAGE "$fivethousand_flag\n\n";
print MESSAGE "$tenthousand_flag\n\n";
print MESSAGE "$fifteenthousand_flag\n\n";
print MESSAGE "Is the ability to store test results from the 
    instrument important?:\n\n";
print MESSAGE "$yes2_flag\n\n";
print MESSAGE "$no2_flag\n\n";
print MESSAGE "Do you plan to purchase within the next 6 months?:\n\n";
print MESSAGE "$yes3_flag\n\n";
print MESSAGE "$no3_flag\n\n";
print MESSAGE "Name: $FORM{name}\n\n";
print MESSAGE "Title: $FORM{title}\n\n";
print MESSAGE "Job Function: $FORM{job}\n\n";
print MESSAGE "Company: $FORM{company}\n\n";
print MESSAGE "Address: $FORM{address}\n\n";
print MESSAGE "City: $FORM{city}\n\n";
print MESSAGE "State: $FORM{state}\n\n";
print MESSAGE "Zip: $FORM{zip}\n\n";
print MESSAGE "Telephone: $FORM{telephone}\n\n";
print MESSAGE "E-mail: $FORM{e-mail}\n\n";



close (MESSAGE);


exit(0);

当我从浏览器点击提交时,我收到此错误:

软件错误:

无法在 \boswinfs05\home\users\web\b465\whl.caadmin\www\HTML-email\SP_ToolKitPROMO_NECA2013\functions\formmailer.cgi 第 117 行,“};”附近的标量赋值中修改减法 (-) 在 \boswinfs05\home\users\web\b465\whl.caadmin\www\HTML-email\SP_ToolKitPROMO_NECA2013\functions\formmailer.cgi 第 180 行缺少右花括号或方括号,在 \boswinfs05\home\ 的行尾语法错误users\web\b465\whl.caadmin\www\HTML-email\SP_ToolKitPROMO_NECA2013\functions\formmailer.cgi 第 180 行,在 EOF 执行 \boswinfs05\home\users\web\b465\whl.caadmin\www\HTML-email \SP_ToolKitPROMO_NECA2013\functions\formmailer.cgi 由于编译错误而中止。

如需帮助,请向本网站的网站管理员发送邮件,提供此错误消息以及错误的时间和日期。

我怎样才能使脚本工作?

任何见解都将不胜感激,或者链接到更深入的教程也很棒。

谢谢!

4

3 回答 3

6

You are using hyphens in those identifiers.

$Magazine-name = $FORM{magazine-name};
$E-mail = $FORM{e-mail};

Change those to underscores.

Enable syntax highlighting in your editor. That will help you catch such errors quickly even before compiling.

于 2013-09-13T15:18:27.483 回答
2

$E-mail并且$Magazine-name不是有效的变量名。变量名 (*) 中只能使用字母、数字和下划线字符。尝试$E_mail$Email

(*) - 没有一点符号表魔法或其他让 Perl 成为只写语言的印象

于 2013-09-13T15:18:33.573 回答
0

提示:

  • 一直用use strict; use warnings;

  • 使用以下内容而不是自己处理 CGI:

    use CGI qw( );
    
    my $cgi  = CGI->new();
    my $form = $cgi->Vars();
    
    # Use $form->{...} instead of $FORM{...}
    
  • if( $form->{magazine} ){
        $magazine_flag ="ON";
    }else{
        $magazine_flag ="OFF";
    }
    
    if( $form->{internet} ){
    ...
    

    可以写成

    $form->{$_} = $form->{$_} ? "ON" : "OFF"
       for qw(
          magazine
          internet
          ...
       );
    
于 2013-09-13T16:19:20.137 回答