简体   繁体   中英

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

' 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 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.

Thank you :)

All of your input fields must be between the opening <form> and closing </form> tags.

Also, the form method should be "post", not "get".

The form action you have as form.cgi . Where does this file live on your server? In typical setups, it might be in the cgi-bin directory, in which case you may want to try action="/cgi-bin/form.cgi" .

Hope those suggestions help.

You are using a lot of variables that don't have values - $mailTo , $subjectLine , $message . I guess that you're used to PHP where variables like those are filled in automatically for you. That's not the case in Perl. You need to extract the form field data yourself.

As you're writing a CGI program, your easiest solution is probably to use the param() function from the CGI module to do this.

use CGI 'param';

my $subjectLine = param('subjectLine'); # etc...

Also, a CGI program needs to send a "Content-type" header before sending any real output. As you are writing plain text, you should use text/plain . The CGI module also has a header() function that will help you here.

# Load both of the functions you're going to use
use CGI ('header', 'param');

# Run this before any other print statements
print header(-type => 'text/plain');

If you're going to be writing CGI programs, then it will be a good idea to take the time to read the documentation for the CGI module . But I'd also recommend reading CGI::Alternatives as CGI is no longer the best way to write web programs in Perl.

Also, in your HTML, your closing </form> tag should come after all of the form inputs.

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.)

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 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.

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.

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 . 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM