Contact Form (POST)

Notice the URL! Unlike GET, the URL doesn't change when you submit.

POST data is sent in the request body, not the URL.

The $_POST Superglobal

<?php
// Check request method first
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = $_POST['name'] ?? '';
    $email = $_POST['email'] ?? '';

    // Validation
    if (empty($name)) {
        $errors[] = 'Name is required';
    }

    // Process if no errors...
}
?>

Current Request Details

Request Method: GET
$_POST contents:
Array
(
)

Form Resubmission Warning

If you refresh this page after submitting, the browser will ask to resubmit the form. This can cause duplicate submissions!

Solution: Use the POST-Redirect-GET pattern:

if (/* form valid */) {
    // Process form...
    header('Location: success.php');
    exit;  // IMPORTANT: stop execution
}

Why POST for Forms?


Previous: GET Example | Back to Module | Next: Combined Example