Broad Network


Simple Form Email Validation and Feedback with Perl

Sending Email with ActivePerl – Part 4

Forward: In this part of the series we look at simple form email validation and feedback with Perl.

By: Chrysanthus Date Published: 28 Jul 2012

Introduction

This is part 4 of my series, Sending Email with ActivePerl. In this part of the series we look at simple form email validation and feedback with Perl. When the Internet user fills an HTML (web) form with the hope that the form information will be sent as email, it is possible that he can make mistakes while filling the form. When the form information is sent to the web server, the information should be validated at the web server to make sure the form was properly filled. If the form was not properly filled, then the information as email will not be sent to the email server; instead, feedback will be sent to the user indicating the error (s) for him to correct and send the form information again. If the form was properly filled, then the information as email will be sent to the email box and a feedback will be sent to the user indicating that his information has been sent.

So if form is not properly filled, feedback is sent to the user; if form is properly filled feedback is still sent to the user, but with good message.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

This article has been written with ActivePerl. Everything said in this article is applicable to traditional Perl. However, with traditional Perl you have to precede your code with something like, #!/usr/bin/perl .

Simple Validation
Required fields are fields of the form that the user must fill. In this article we shall only check if a required field was filled or not. Certain types of data like email address and URL have particular formats. During validation, it is possible to check if an email address or URL was properly filled, based on its format. We shall not go into such depth in this article.

We shall use the form code given in part 2 of this series. In that code, only the first name, email and message fields are required. In this article we shall only check if anything was filled in these fields or not. In order to do this we shall use the return string variables returned from the query object.

Remember, the script at the web server that receives the form information (query object) is the same script that will do validation, send the email and send either feedback to the user.

Validation Code
We continue to develop the script we had in the previous parts of the series. There are three sub segments in the validation code.

The First Sub Code Segment
The first sub segment is:

#Validation and Error Feedback
my $anyError = "false";
my $fnameErr = "false";
my $emailErr = "false";
my $msgErr = "false";

Four error variables are initialized here to false. If any of the required fields was not filled, $anyError would be set to true. If the first name field was not filled, $fnameErr would be set to true. If the sender’s email field was not filled, $emailErr would be set to true. If the message field was not filled, $msgErr would be set to true.

The Second Sub Segment
Next is the second sub segment:

if ($fnameVal eq "")
    {
        $fnameErr = "true";
        $anyError = "true";
    }
if ($emailVal eq "")
    {
        $emailErr = "true";
        $anyError = "true";
    }
if ($msgVal eq "")
    {
        $msgErr = "true";
        $anyError = "true";
    }

There are three if-constructs here. Each one checks if the return string from the query object of the corresponding required field is empty. If it is empty, it sets its corresponding error variable to true and the generalized error variable, $anyError to true.

The Third Sub Segment
The third sub segment will check if the value of $anyError became true. If it became true it means there is one or more errors. Under this condition, the next sub segment will send a web page to the browser indicating that one or more errors have occurred and also indicating the particular error or particular errors. This is the sub-segment:

if ($anyError eq "true")
    {
        print "<html>";
        print "<head>";
        print "</head>";
        print "<body>";
        print "<h3>One or more fields have errors!</h3>";
        if ($fnameErr eq "true")
            {
                print "<span>The first name field cannot be empty!</span><br />";
            }
        if ($emailErr eq "true")
            {
                print "<span>The email field cannot be empty!</span><br />";
            }
        if ($msgErr eq "true")
            {
                print "<span>The message field cannot be empty!</span><br />";
            }
        print "</body>";
        print "</html>";
    }

Before the first internal if-construct the code sends an H3 HTML element indicating that there is one or more errors. There are three internal if-constructs. Any of them will send an error message if its error variable is false. Read through the code. Each error variable corresponds to a particular error message and a particular form field.

When the script is called, it can send back one of two feedbacks. It can send back the error messages as explained above or it can send the confirmation message as explained below.

Confirmation
The code segment to send the email to the email server and to send confirmation to the user that the email has been sent is as follows:

#the following should send the mail if validation was OK
if ($anyError eq "false")
    {
        open(SENDMAIL, "|/usr/lib/sendmail -oi -t")
                                or die "Can't fork for sendmail: $!\n";
        print SENDMAIL $emailMessage;
        close(SENDMAIL)     or warn "sendmail didn't close properly";

        print "<html>";
        print "<head>";
        print "</head>";
        print "<body>";
        print "<h3>Thank You.</h3>";
        print "<h4>Your Message has been sent.</h4>";
        print "</body>";
        print "</html>";
    }

It is in an if-construct. The condition of the if-construct checks if the generalized error variable ($anyError) is false. If it is false it means all the fields from the form were valid and the validation code segment did not change the value of this variable to true. And so the email can be sent and confirmation message sent back to the user. Inside the if-block, the first three statements send the email; we saw these statements in the part 2 of the series. After those three statements a confirmation page is sent back to the user in print statements.

After the statement, “print "Content-Type: text/html\n\n";” above in the complete code, any print function (statement) sends its argument to the browser. It is either feedback that is sent back to the browser, not both; if-constructs are used to achieve this.

The HTML form code and the complete ActivePerl script can be gotten from the link below.

After downloading unzip. I am not responsible for any error you may encounter with the code. The complete ActivePerl script contains the main code for sending emails with Perl.

We have come to the end of the series. I hope after designing a web site you will be in the position to design a Perl script to send form info as email. You should also be able to do other email stuffs with Perl. I hope you appreciated the series.

Chrys

http://www.broad-network.com/ChrysanthusForcha/aperlscript.zip

Related Links

Major in Website Design
Web Development Course
HTML Course
CSS Course
ECMAScript Course

Comments

Become the Writer's Fan
Send the Writer a Message