我正在尝试使用PERL / CGI创建一个表单,我想在同一CGI文件中处理以该表单引入的数据 [英] I am trying to create a form with PERL/CGI, and I would like to process the data introduced in that form within the same CGI file

查看:46
本文介绍了我正在尝试使用PERL / CGI创建一个表单,我想在同一CGI文件中处理以该表单引入的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

’我正在尝试使用PERL / CGI创建一个表单,我想在同一CGI文件中处理该表单中引入的数据。这就是我在HTML5端的代码所拥有的……。’

' I am trying to create a form with PERL/CGI, and I would like to process the data introduced in that form within the same CGI file. This is what I have for my code on the HTML5 side….'

<body>
   <form action="form.cgi" method="get">
</form>

<h1>Feedback Form</h1>
<p>Please fill out the entire feedback form.</p>
<table>
<tr>
<td><b>To (recipient's e-mail address):</b></td>
</tr>
<tr>
<td><input type = "text" name = "mailTo" size = "40"  /></td>
</tr>
<tr>
<td><b>From (your e-mail address):</b></td>
</tr>
<tr>
<td><input type = "text" name = "mailFrom" size = "40" /></td>
</tr>
<tr>
<td><b>Enter a subject:</b></td>
</tr>
<tr>
<td><input type = "text" name = "subjectLine" size = "40" /></td>
</tr>
<tr>
<td><b>Enter your message:</b></td>
</tr>
<tr>
<td><textarea name = "message" rows = "10" cols = "50"></textarea></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td><input type = "submit" name = "sbutton" value = "Submit" />
<input type = "reset" value = "Reset" /></td>
</tr>
</table>

<br><br><br><br><br> 



</div>
</body></html>

PERL / CGI表格代码

PERL/CGI form CODE

#!/usr/bin/perl
use Modern:: Perl;
use Mail::Sendmail; 

my $mailFrom = email@email'; 

my $subjectLine = "Sample Subject:l 
my $message = "Sample Message!"; 
my %mail = ( To      => $mailTo, 
             From    => $mailFrom, 
             Subject => $subjectLine,
             Message => $message, 
             'Content-Type' => 'text/plain' 
           ); 

if ( sendmail %mail ) 
{
  print "Sucessfully sent mail to $mailTo. Check you box! \n";
}
else 
{
  print "Error sending mail: $Mail::Sendmail::error \n";
}

我花了几个小时试图弄清楚这段代码,请任何人帮帮我。为这两种不同的代码制作了两个不同的文件。我觉得我缺少了一些东西,或者如果我缺少了一些小东西。

I have spent over a couple of hours trying to figure out this code. Please can anyone help me out. I had made two different files for these two different codes. I feel that I am missing something or if I am missing something small.

谢谢:)

推荐答案

使用perl CGI表单本身很容易,这是使用您的HTML代码的一个小示例。(顺便说一下,所有表单字段都必须包含在< form> 标签,而不是您的方式。是的,表单方法是POST。)

It's easy to have a perl CGI form call itself, here's a small example using your HTML code. (By the way, all the form fields have to be enclosed within the <form> tags, not the way yours is. And yes the form method is POST.)

此CGI文件的功能必须根据访问方式的不同而不同,首先需要打印输出带有空表单字段的HTML。这是代码中的 $ html变量,然后在提交时需要接收e形成参数,并对数据进行处理。要通过电子邮件发送,请调用 mailForm 子例程。我的服务器上没有sendmail,因此我只打印了示例中的表单数据。下面的代码将其邮寄。

This CGI file has to function differently depending on how it's accessed. First it needs to print out HTML with empty form fields. This is the "$html" variable in the code. Then when it's submitted, it needs to receive the form parameters, and do something with the data. To email it, call the mailForm subroutine. I don't have sendmail on my server, so I just print out the form data in the example. The code below mails it.

CGI脚本需要说明只是作为网页被访问与作为表单动作被调用之间的区别。为此,它将检查名为 check的隐藏表单字段。如果定义了检查,则表示表单已提交,并且有数据需要收集和处理。如果未定义检查,它只会打印出空白表格字段。

The CGI script needs to tell the difference between just being visited as a web page, and being called as a form action. To do this, it checks for a hidden form field called "check." If "check" is defined, that means the form was submitted and there's data to collect and process. If "check" isn't defined, it just prints out the empty form fields.

执行此操作的方法可能更优雅,但我只是想演示一个CGI文件,该文件将处理自己提交的数据,以防将来有人对它感兴趣。确保文件的名称与表单的操作相同。这是我的示例代码, form.cgi

There are probably more elegant ways to do this, but I just wanted to demonstrate a CGI file processing its own submitted data, in case anyone is interested in the future. Make sure the file has the same name as the form's action. Here's my sample code, form.cgi:

#!/usr/bin/perl        
use CGI::Carp qw(fatalsToBrowser set_message warningsToBrowser);
use CGI qw(:standard);
use strict;                     
use warnings;   

print header(); 

my $cgi = CGI->new();

my $check;
$check = $cgi->param('check') if defined $cgi->param('check');

my $newhtml = '';

if ($check eq "yes") { #collect form data and build email subroutine
my $fromemail = $cgi->param('mailFrom');
my $toemail = $cgi->param('mailTo');
my $subject = $cgi->param('subjectLine');
my $message = $cgi->param('messageBody');

$newhtml = qq{
<html>
<body>
<b>Email sent!</b>
<br>
The from email is from $fromemail<br>
The to email is to $toemail<br>
The subject is $subject<br>
The message is $message<br>
</body>
</html>
 };

sub mailForm {
open(MAIL, "|/usr/sbin/sendmail -t");

print MAIL "To: $toemail\n";
print MAIL "From: $fromemail\n";
print MAIL "Subject: $subject\n\n";

print MAIL $message;

close(MAIL);
}


} #end of if check

my $html = qq{
<html>
<body>
   <form action="form.cgi" method="POST">

<h1>Feedback Form</h1>
<p>Please fill out the entire feedback form.</p>
<table>
<tr>
<td><b>To (recipient's e-mail address):</b></td>
</tr>
<tr>
<td><input type = "text" name = "mailTo" size = "40" maxlength = "50"  /></td>
</tr>
<tr>
<td><b>From (your e-mail address):</b></td>
</tr>
<tr>
<td><input type = "text" name = "mailFrom" size = "40" maxlength = "50" /></td>
</tr>
<tr>
<td><b>Enter a subject:</b></td>
</tr>
<tr>
<td><input type = "text" name = "subjectLine" size = "40" maxlength = "50"></td>
</tr>
<tr>
<td><b>Enter your message:</b></td>
</tr>
<tr>
<td><textarea name = "messageBody" rows = "10" cols = "50" maxlength = "300"></textarea></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td><input type = "submit" name = "sbutton" value = "Submit" />
<input type = "reset" value = "Reset" />
<input type = "hidden" name = "check" value = "yes">
</td>
</tr>
</table>

</form>

<br>

</body></html>
};

#this prints the regular page if no form is submitted
if (!defined $check) {
print $html;
}

#this mails the form data and prints a confirmation page
else {
print $newhtml; 
mailForm();
}

exit 0;

这篇关于我正在尝试使用PERL / CGI创建一个表单,我想在同一CGI文件中处理以该表单引入的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆