Skip to content
Home Tutorials Roadmaps Courses
Log in Join free
Tutorials PHP Handling Forms
PHP Intermediate FREE

Handling Forms

Lesson 1 of 17 Intermediate Interactive

PHP makes it easy to handle form submissions from HTML forms. When a form is submitted, the data becomes available in PHP superglobal variables.

GET vs POST

  • $_GET — Data from URL query parameters (visible in URL)
  • $_POST — Data from form submissions using POST method (not visible in URL)
  • $_REQUEST — Contains both GET and POST data

Form Handling Steps

  1. Create the HTML form with method="POST"
  2. Process data with PHP when the form is submitted
  3. Validate the input (check if it meets requirements)
  4. Sanitize the input (remove harmful characters)

Tip: Always validate and sanitize user input to prevent security vulnerabilities like XSS and SQL injection.

Syntax

PHP
<!-- HTML Form -->
<form action="process.php" method="POST">
    <input type="text" name="name" required>
    <input type="email" name="email" required>
    <button type="submit">Submit</button>
</form>

<?php
// process.php

if ($_SERVER["REQUEST_METHOD"] === "POST") {
    $name = htmlspecialchars(trim($_POST["name"] ?? ""));
    $email = filter_var($_POST["email"] ?? "", FILTER_SANITIZE_EMAIL);

    if (empty($name)) {
        echo "Name is required.";
    } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        echo "Invalid email.";
    } else {
        echo "Welcome, $name!";
    }
}
?>
Processing HTML Forms
PHP
<?php
$errors = [];

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

    if ($name === "") $errors[] = "Name is required";
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $errors[] = "Invalid email";
    }

    if (empty($errors)) {
        echo "Welcome, " . htmlspecialchars($name) . "!
";
        echo "We emailed " . htmlspecialchars($email) . "
";
    } else {
        foreach ($errors as $error) {
            echo "Error: " . $error . "
";
        }
    }
}
?>
<form method="post">
    <input name="name" placeholder="Your name">
    <input name="email" type="email" placeholder="Email">
    <button type="submit">Submit</button>
</form>

Practice

1
Exercise

Create a PHP form handler that validates that both name and email fields are not empty.

Answer
<?php
$name = trim($_POST["name"] ?? "");
$email = trim($_POST["email"] ?? "");

if (empty($name) || empty($email)) {
    echo "Both fields are required.";
} else {
    echo "Welcome, $name!";
}
?>
2
Exercise

Use filter_var() to validate an email address and display appropriate messages.

Answer
<?php
$email = $_POST["email"] ?? "";
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
    echo "Valid email: $email";
} else {
    echo "Invalid email address.";
}
?>

Quick Quiz

1

Which superglobal contains data from a POST form submission?

$_POST contains data sent via the HTTP POST method, typically from HTML forms.

2

What does htmlspecialchars() do?

htmlspecialchars() converts characters like < > & " to HTML entities to prevent XSS attacks.

3

Which filter validates an email address?

FILTER_VALIDATE_EMAIL checks if the value is a valid email address format.

Interview Questions

$_GET data is appended to the URL as query parameters (visible to users), while $_POST data is sent in the request body (not visible in the URL). $_GET is limited in size and should not be used for sensitive data. $_POST is preferred for form submissions that modify data.

Unsanitized input can contain malicious code like SQL injection queries, XSS scripts, or file inclusion attacks. Sanitization removes or escapes harmful characters, while validation ensures data matches expected formats. Together, they protect your application from common security vulnerabilities.