我认为可能就是一切!
我基本上不知道自己在做什么。我之前从未写过.cgi文件,但我确实编写了基于几个教程的脚本... 其中一个就是这一个...... http://www.yourhtmlsource.com/cgi/processingforms.html
我想要一个Perl脚本来处理一个html表单,收集用户的数据并发送到电子邮件,并在用户点击SUBMIT时返回感谢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的执行中止。
如需帮助,请发送邮件至本网站的网站管理员,提供此错误消息以及错误的时间和日期。
如何让脚本有效?
任何见解都会受到高度赞赏,或者链接到更深入的教程也会很棒。
谢谢!
答案 0 :(得分:6)
您在这些标识符中使用连字符。
$Magazine-name = $FORM{magazine-name};
$E-mail = $FORM{e-mail};
将这些更改为下划线。
在编辑器中启用语法突出显示。这将有助于您在编译之前快速捕获此类错误。
答案 1 :(得分:2)
$E-mail
和$Magazine-name
不是有效的变量名称。只能在变量名称(*)中使用字母,数字和下划线字符。试试$E_mail
或$Email
。
(*) - 没有一个小符号表魔法或其他黑客,留下Perl作为只写语言的印象
答案 2 :(得分: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
...
);