Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Handling Form Submission in PHP

Introduction

Form submission is a fundamental part of web development. It allows users to send data to the server, which can be processed and stored. In this tutorial, we will cover how to handle form submission in PHP from start to finish, with detailed explanations and examples.

Creating the HTML Form

First, let's create a simple HTML form that allows users to submit their name and email address.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Form Submission</title>
</head>
<body>
    <form action="submit.php" method="POST">
        <label for="name">Name:</label>
        <input type="text" id="name" name="name"><br><br>
        <label for="email">Email:</label>
        <input type="email" id="email" name="email"><br><br>
        <input type="submit" value="Submit">
    </form>
</body>
</html>

Handling Form Submission in PHP

Next, we need to create a PHP script to process the form data. This script will be named submit.php.

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = $_POST['name'];
    $email = $_POST['email'];

    // Validate input
    if (empty($name) || empty($email)) {
        echo "Name and email are required.";
    } else {
        echo "Name: " . htmlspecialchars($name) . "<br>";
        echo "Email: " . htmlspecialchars($email);
    }
}
?>

Validating Form Data

It's crucial to validate the form data to ensure it meets the required criteria before processing it. In this example, we check if the name and email fields are not empty.

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = $_POST['name'];
    $email = $_POST['email'];

    // Validate input
    if (empty($name) || empty($email)) {
        echo "Name and email are required.";
    } else {
        // Sanitize input
        $name = filter_var($name, FILTER_SANITIZE_STRING);
        $email = filter_var($email, FILTER_SANITIZE_EMAIL);

        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            echo "Invalid email format.";
        } else {
            echo "Name: " . htmlspecialchars($name) . "<br>";
            echo "Email: " . htmlspecialchars($email);
        }
    }
}
?>

Storing Form Data

After validating the form data, you can store it in a database. Below is an example of how to insert the data into a MySQL database.

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = $_POST['name'];
    $email = $_POST['email'];

    // Validate input
    if (empty($name) || empty($email)) {
        echo "Name and email are required.";
    } else {
        // Sanitize input
        $name = filter_var($name, FILTER_SANITIZE_STRING);
        $email = filter_var($email, FILTER_SANITIZE_EMAIL);

        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            echo "Invalid email format.";
        } else {
            // Create connection
            $conn = new mysqli($servername, $username, $password, $dbname);

            // Check connection
            if ($conn->connect_error) {
                die("Connection failed: " . $conn->connect_error);
            }

            $sql = "INSERT INTO users (name, email) VALUES ('$name', '$email')";

            if ($conn->query($sql) === TRUE) {
                echo "New record created successfully";
            } else {
                echo "Error: " . $sql . "<br>" . $conn->error;
            }

            $conn->close();
        }
    }
}
?>

Conclusion

Handling form submission in PHP involves creating an HTML form, processing the form data with a PHP script, validating the input, and optionally storing the data in a database. By following the steps outlined in this tutorial, you can effectively handle form submissions in your PHP applications.