PHP Form Handling: A Complete Example

Learn how to create and handle a complete form in PHP, including processing user input, validation, and displaying feedback.

Creating a Simple Form

We will start with a simple form that collects a user's name, e-mail, and message. The form will be submitted to a PHP script for processing.

<form action="process-form.php" method="post">
    <label for="name">Name:</label>
    <input type="text" id="name" name="name" required>

    <label for="email">E-mail:</label>
    <input type="email" id="email" name="email" required>

    <label for="message">Message:</label>
    <textarea id="message" name="message" required></textarea>

    <input type="submit" value="Submit">
</form>

Processing Form Data in PHP

In the `process-form.php` script, we will handle the submitted data. We will validate the input and display a confirmation message or errors if the input is invalid.

<?php
// Initialize variables
$name = $email = $message = "";
$nameErr = $emailErr = $messageErr = "";

// Check if the form was submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Validate name
    if (empty($_POST["name"])) {
        $nameErr = "Name is required.";
    } else {
        $name = htmlspecialchars(trim($_POST["name"]));
    }

    // Validate e-mail
    if (empty($_POST["email"])) {
        $emailErr = "E-mail is required.";
    } else {
        $email = htmlspecialchars(trim($_POST["email"]));
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $emailErr = "Invalid e-mail format.";
        }
    }

    // Validate message
    if (empty($_POST["message"])) {
        $messageErr = "Message is required.";
    } else {
        $message = htmlspecialchars(trim($_POST["message"]));
    }
}
?>

Displaying Error Messages and Confirmation

Here's how to show error messages if validation fails or a confirmation message if everything is correct:

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (empty($nameErr) && empty($emailErr) && empty($messageErr)) {
        echo "<p class='text-green-500'>Thank you, " . $name . ". Your message has been received!</p>";
    } else {
        echo "<p class='text-red-500'>" . $nameErr . "</p>";
        echo "<p class='text-red-500'>" . $emailErr . "</p>";
        echo "<p class='text-red-500'>" . $messageErr . "</p>";
    }
}
?>

Full Example: `process-form.php`

Here is the complete `process-form.php` file that processes the form submission:

<?php
// Initialize variables
$name = $email = $message = "";
$nameErr = $emailErr = $messageErr = "";

// Check if the form was submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Validate name
    if (empty($_POST["name"])) {
        $nameErr = "Name is required.";
    } else {
        $name = htmlspecialchars(trim($_POST["name"]));
    }

    // Validate e-mail
    if (empty($_POST["email"])) {
        $emailErr = "E-mail is required.";
    } else {
        $email = htmlspecialchars(trim($_POST["email"]));
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $emailErr = "Invalid e-mail format.";
        }
    }

    // Validate message
    if (empty($_POST["message"])) {
        $messageErr = "Message is required.";
    } else {
        $message = htmlspecialchars(trim($_POST["message"]));
    }

    // Display confirmation or errors
    if (empty($nameErr) && empty($emailErr) && empty($messageErr)) {
        echo "<p class='text-green-500'>Thank you, " . $name . ". Your message has been received!</p>";
    } else {
        echo "<p class='text-red-500'>" . $nameErr . "</p>";
        echo "<p class='text-red-500'>" . $emailErr . "</p>";
        echo "<p class='text-red-500'>" . $messageErr . "</p>";
    }
}
?>

Conclusion

Handling forms in PHP involves creating the form, processing the data, validating input, and providing feedback to users. This complete example shows how to build and manage a form, ensuring that users receive proper validation and confirmation messages.