Articles: A simple Form processor
This article includes some of the basics for a Perl form processor. It will give both options, to save to a file, or to send an email. It is ment to be used with the POST method.
Added: 2003-07-13 22:25:05 - Modified: 2005-08-03 17:51:21 - Level: Intermediate
![]()
Recommend this article to a friend.
Toggle more
The first thing we need to do in our CGI script is to tell the server what do do with it,
#!/usr/bin/perl
This has to be the first line in the script. The next thing we do is to supply a set of valid HTTP headers, for now we'll keep it simple, so we tell the browser that the information it get is in plain text.
print "Content-type: text/plain\n\n";
This is the basis we'll find in any perl script, a line that tells the webserver where the perl executable is, and a header, notice the \n\n, since that is the proper form of a HTTP result. Now you can experiment a little, try to add eg print "mytext"; and see that you get a result.
Back to the form processor. we now add some configuration directives.
$method = 'file'; # either mail or file
# For file storing
$outputfile = "output.txt";
# For email sending
$mailprog = '/usr/sbin/sendmail';
$recipients = 'user@host';
$from = 'user@host';
This should be pretty self explainatory, the method variable include either mail or file, depending on what we want it to do, and the configuration following depending on the method we choose.Now we start the real processing:
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;
}
This is some harder to understand, the good thing is that we don't have to do so much about it. First it read the information from STDIN, the standard input. it will read all the data into the $buffer variable, before it split the string into the array @pairs. this array now contain the keys and the values you submitted. The next thing the snippet does, is to decode the information that is stored,
Now we want to start deciding what the information that is sent should look like. we create the variable $string.
$string = "";
foreach $key (keys(%FORM)) {
$string .= "$key: $FORM{$key}
";
}
$string .= "IP:".$ENV{'REMOTE_ADDR'}."
";
$string .= "Form: ".$ENV{'HTTP_REFERER'}."
";
Now we add the information from the form to the string, looking like key: value. after that we add the IP addresse of the sender, and the addresse the form was located at.
Now we have to do something about the string, now it is just internal to the perl script, first we are going to look at storing the data in a file.
# Store to file
if($method eq 'file')
{
open(FP, ">>$outputfile");
print FP "#################
";
print FP $string;
print FP "#################
";
close(FP);
}
we start by checking if the method we supplied in the beginning tell it to store it to a file. After that we open a File Pointer, in short FP, this is just a name and we can call it anything we like. We open the file $outputfile. Here you should notice the >>, the >> tells Perl to open the file, and put the file cursor at the end of the file, or appending. This means that every time we send something to the CGI, it will add data to the file, our other option was to use >, then it would overwrite the previous data, we don't want that. After this it write to the file, this is done the same way as we would output anything to the browser, just that we tell it to send to FP instead, notice the syntax. After that we close the file.
# Send email
if($method eq 'mail')
{
open(MAIL, "|$mailprog -t") or dien("Can't access sendmail binary");
print MAIL "To: $recipients
";
print MAIL "From: $from
";
print MAIL "Reply-to: $from
";
print MAIL "Subject: Form Data
";
print MAIL $string;
close(MAIL);
}
We first check if we want it to send a mail, as we did with the file. then we open a pipe to the mail application, using the pipe character | . Notice that we use this about the same way that we did when storing to a file, we just have to supply the application with a correct set of headers. To round it up, we use a subroutine called dien if the mail isn't sent, we add this to the bottom, along with a message that the form was successfully processed.
print "Form submitted";
sub dien
{
my($errmsg) = @_;
print $errmsg;
exit;
}
To use the script, we use a simple form in HTML. an example of this is:
<form action="/cgi-bin/formsend.pl" method="post">
<input type="text" name="ourtest">
<input type="submit" value="Send">
</form>
And that was it, it wasn't that hard was it? the full script can be found at http://www.kfwebs.net/scripts/form-processor.pl.txt, good luck with your Perl scripting
Related articles:
Processing a form => Sending it through email (PHP)
[Sitemap]


