PHP DOCUMENTS

01

contact_form.php

This document is created to handle the data sent by the contact form located on the contact page. Where it is formatted and sent as an email to the address of info@example.com. Example shown below. The fields that were defined by the contact form on contact.html are shown in yellow.

Add reauired code to contact_form.php
Indentation is not as important here either, just make sure not to forget any semicolons or braces.

<?php
    if (isset($_POST['submit'])) {

        $name = $_POST['name'];
        $mailFrom = $_POST['mail'];
        $subject = $_POST['subject'];
        $message = $_POST['message'];

        $to = "info@example.com";
        $headers = "From: ".$mailFrom;
        $txt = "You Have Recieved an e-mail from ".$name.".\n\n".$message;

        if (!Filter_var($mailFrom, FILTER_VALIDATE_EMAIL)) {
            header("Location: https://example.com/contact/form_error");
        }
        else {
            if (empty($name) || empty($mailFrom) || empty($subject) || empty($message)) {
                header("Location: https://example.com/contact/form_error");
            }
            else {
                mail($to, $subject, $txt, $headers);
                header("Location: https://example.com/contact/form_passed");
            }
        }
    }
?>

Shown below, is the contact from on contact.html were the following fields were defined (submit, name, mail, subject and message). Which were processed by the php document above.

<div class="form-container">
    <form method="POST" action="contact_form.php">
        <fieldset>
            <legend>
                Contact Form
            </legend>
            <div class="form-fields">
                <label>Name / Alias</label>
                <input type="text" name="name" required placeholder="your name">
                <label>Contact Information</label>
                <input type="text" name="mail" required placeholder="phone/email">
                <label>Subject</label>
                <input type="text" name="subject" required placeholder="subject of the message">
                <label>Message</label>
                <textarea name="message" required placeholder="Message"></textarea>
                <button type="submit" name="submit" class="btn2">SEND MESSAGE</button>
            </div>
        </fieldset>
    </form>
</div>