Broad Network


Simple Form Email Validation and Feedback with PHP

Sending Email with PHP – Part 4

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

By: Chrysanthus Date Published: 29 Jul 2012

Introduction

This is part 4 of my series, Sending Email with PHP. In this part of the series we look at simple form email validation and feedback with PHP. 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 (at the email server) 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. It is possible that the form will be properly filled, but something will go wrong with the sending process of the email to the email server. The PHP script initiates this with the mail function. If the mail function returns false, it means the sending email process has failed. The user should be informed (feedback) about this.

When you click the submit button at the browser, the data set (form information) is sent to the web server. The PHP script at the web server will then form and send the email to the email server.

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.

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 address 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 $_POST array.

Remember, the script at the web server that receives the form information is the same script that will do validation, send the email and send 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 code segments in the validation code.

The First Sub Code Segment
The first sub segment is:

#Validation and Error Feedback
$anyError = "false";
$fnameErr = "false";
$emailErr = "false";
$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 Code Segment
Next is the second sub segment:

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

There are three if-constructs here. Each one checks if the return string from the $_POST array 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 Code Segment
The third sub code 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 new 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 code segment:

if ($anyError == "true")
    {
        echo "<html>";
        echo "<head>";
        echo "</head>";
        echo "<body>";
        echo "<h3>One or more fields have errors!</h3>";
        if ($fnameErr == "true")
            {
                echo "<span>The first name field cannot be empty!</span><br />";
            }
        if ($emailErr == "true")
            {
                echo "<span>The email field cannot be empty!</span><br />";
            }
        if ($msgErr == "true")
            {
                echo "<span>The message field cannot be empty!</span><br />";
            }
        echo "</body>";
        echo "</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. Each 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.

Sending the Email
The code segment to send the email to the email server and to send feedback to the user as to whether the script successfully sent the email is:

if ($anyError == "false")
    {
        if (mail($to, $subject, $message, $additional_headers))
            {
                echo "<html>";
                echo "<head>";
                echo "</head>";
                echo "<body>";
                echo "<h3>Thank You.</h3>";
                echo "<h4>Your Message has been sent.</h4>";
                echo "</body>";
                echo "</html>";
            }
        else
            {
                echo "<html>";
                echo "<head>";
                echo "</head>";
                echo "<body>";
                echo "<h3>Sending Mail Problem</h3>";
                echo "<h4>Email could not be sent! Contact your system administrator.</h4>";
                echo "</body>";
                echo "</html>";
            }
    }

It is in an if-construct. The condition of the main 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 feedback message sent back to the user to tell him so. Inside the main if-block, you have another if-block that gives the sending email command and issues feedback accordingly. Read through the code.

After calling a script from the browser, any echo construct in the script sends its argument to the same new page at the browser.

The complete PHP script and the HTML form code 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 PHP script contains the main code for sending emails with PHP.

Let us take a break here and continue in the next part of the series.

Chrys

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

Related Links

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

Comments

Become the Writer's Fan
Send the Writer a Message