How to Create an HTML/PHP Contact Form – Processing Form Data with PHP

Gears - image by Alvaro Heinzen

In the previous tutorial – creating HTML PHP contact form – we successfully created a basic layout for the contact form. By the way, I’ve also added 2 screencasts to the post for those of you guys who really prefer video content to textual. So, if you wish to watch instead of read, you can do it. In other words you can choose now which way to go. Alternatively, you can both watch and read (or the other way around) just for better understanding. By the way, I’d really appreciate if you subscribe to our YouTube channel and like our videos. 🙂 Now let’s make our contact form actually do something. First off, it’s a good idea to understand how it works.

Under the hood

So, what happens after a user hits the “Submit” button of our contact form?

1)Let’s go step-by-step. The user fills in the form and hits the button:

2) Remember we assigned the action and method attributes to the form tag? Well, here’s where they come into play. The action attribute specifies that all the form data should be submitted to the send-mail.php file. Method="POST" means that all the values will be stored in an associative array called $_POST

3) Now let’s illustrate the data that the server receives from the contact form (screenshot below)

Here’s how the $_POST array looks like after we send the message. Anything familiar? Hell yeah!
The keys of the array (on the left side) match the names we specified in our html contact form. Each key contains a value – and the value is the data that was submitted (values on the right side of the array).
Now let’s move on and process this data with the send-mail.php file

Creating PHP file for the contact form

Below is the complete code of the send-mail.php file

<?php
    $mail_to = 'webdesy@gmail.com'; // specify your email here

    // Assigning data from the $_POST array to variables
    $name = $_POST['sender_name'];
    $mail_from = $_POST['sender_email'];
    $phone = $_POST['sender_phone'];
    $message = $_POST['sender_message'];

    // Construct email subject
    $subject = 'www.mysite.com Message from visitor ' . $name;

    // Construct email body
    $body_message = 'From: ' . $name . "\r\n";
    $body_message .= 'E-mail: ' . $mail_from . "\r\n";
    $body_message .= 'Phone: ' . $phone . "\r\n";
    $body_message .= 'Message: ' . $message;

    // Construct email headers
    $headers = 'From: ' . $mail_from . "\r\n";
    $headers .= 'Reply-To: ' . $mail_from . "\r\n";

    $mail_sent = mail($mail_to, $subject, $body_message, $headers);

    if ($mail_sent == true){ ?>
        <script language="javascript" type="text/javascript">
        alert('Thank you for the message. We will contact you shortly.');
        window.location = 'contact-form.html';
        </script>
    <?php } else { ?>
    <script language="javascript" type="text/javascript">
        alert('Message not sent. Please, notify the site administrator admin@admin.com');
        window.location = 'contact-form.html';
    </script>
    <?php
    }
?>

Now let’s dig it a bit. First, we create the $mail_to variable, which basically stores the recipient’s email address. Next, we create 4 variables ($name, $mail_from, $phone, $message) and assign the corresponding data from the $_POST array to each of them.

$subject = 'www.mysite.com Message from visitor ' . $name;

The email’s subject consists of two parts: static text – www.mysite.com Message from visitor, and the $name variable, which is basically the visitor’s name. So when you receive this email its subject would be “www.mysite.com Message from visitor John Smith”

In the following two lines, we construct the headers of our email message. The “From” parameter indicates the origin of the email, “Reply-to” is the visitor’s email address.

Now goes the line of code, which actually sends the email message. Notice that we assign the mail() function to the $mail_sent variable. What for? When the mail() function is executed, the server will respond either with True or False, depending on the fact if the email was sent or not. With the help of the PHP “if ... else” statement, we can assign a specific action if the email succeeded or not. And that’s exactly what we do next:

    if ($mail_sent == true){ ?>
        <script language="javascript" type="text/javascript">
        alert('Thank you for the message. We will contact you shortly.');
        window.location = 'contact-form.html';
        </script>
    <?php } else { ?>
    <script language="javascript" type="text/javascript">
        alert('Message not sent. Please, notify site administrator admin@admin.com');
        window.location = 'contact-form.html';
    </script>
    <?php
    }

If the $mail_sent variable is equal to “true”, then we echo a success javascript message, otherwise – echo a fail message.

We got to the end of the second tutorial, which I hope was helpful for you. If you got any questions or ideas, don’t hesitate to leave a comment and we will try to assist you. You can download both files for the first tutorial and current one at the following link HTML PHP Contact Form Files – Part 2

And if you feel like watching a movie now, here you go 🙂

About The Author

Vitaliy Kolos

If you need assistance with SEO, Google Ads or web design, contact Vitaliy Kolos on the Get in Touch page.