Read HTML form data in PHP with $_GET and $_POST

In PHP, $_GET contains URL query parameters and $_POST contains fields from a conventional POST form body. An input’s name becomes the array key. The examples check the type and length of each value, then display the input as text in HTML. PHP Manual · $_GET PHP Manual · $_POST

Choose the right source for GET and POST

Use GET for read-only operations such as searches and filters when the result URL should be shareable. Use POST for submissions that change server state. POST alone provides no encryption. Use HTTPS for real applications and keep sensitive values out of URLs. If a POST URL includes ?page=2, PHP still puts that parameter in $_GET.

Item GET form POST form
Typical data location URL query Request body
PHP input in this example $_GET['color'] $_POST['color']
Sharing the URL Includes query Excludes body

action names the handler URL and method chooses the request method. name="color" supplies the submitted key. An id alone does not send a value. Disabled fields and unchecked checkboxes are typically omitted. Read from the specific array when the source matters. PHP Manual · Dealing with Forms

Run the GET form and inspect the URL

Save the two UTF-8 PHP files below in the same practice folder. With PHP 8.x, run the command from that folder. If port 8000 is in use, use the same replacement port in the command and URLs. Stop the server with Ctrl+C when done. PHP Manual · Built-in Web Server

php -S 127.0.0.1:8000
<?php
declare(strict_types=1);
header('Content-Type: text/html; charset=UTF-8');
$raw = $_GET['color'] ?? '';
$error = '';
$color = '';
if (!is_string($raw)) {
    http_response_code(422);
    $error = 'Enter a single text value for color.';
} else {
    $color = trim($raw);
    if (strlen($color) > 120 || preg_match('//u', $color) !== 1) {
        $color = '';
        http_response_code(422);
        $error = 'Color must be valid UTF-8 and no more than 120 bytes.';
    }
}
function e(string $value): string {
    return htmlspecialchars($value, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
}
?>
<!doctype html>
<html lang="en">
<head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>GET color preview</title></head>
<body>
<form method="get" action="get-form.php">
  <label>Favorite color <input name="color" value="<?= e($color) ?>"></label>
  <button type="submit">Check</button>
</form>
<?php if ($error !== ''): ?>
<p><?= e($error) ?></p>
<?php elseif ($color === ''): ?>
<p>Enter a color.</p>
<?php else: ?>
<p>Entered color: <?= e($color) ?></p>
<?php endif; ?>
</body>
</html>

Save the GET code as get-form.php. With the server running, open http://127.0.0.1:8000/get-form.php and enter blue. The URL ends in ?color=blue. A missing value shows a prompt, while the string 0 is displayed as zero. PHP has already decoded the query, so skip another urldecode().

Read and validate the POST body

Save the POST example as post-form.php. Submit both fields at http://127.0.0.1:8000/post-form.php. The values appear in the current response. In the browser’s Network panel, look for a POST request with color and sport in the form body. Copying the URL does not copy that body.

<?php
declare(strict_types=1);
header('Content-Type: text/html; charset=UTF-8');
$submitted = ($_SERVER['REQUEST_METHOD'] ?? '') === 'POST';
$color = '';
$sport = '';
$error = '';
if ($submitted) {
    $rawColor = $_POST['color'] ?? '';
    $rawSport = $_POST['sport'] ?? '';
    if (!is_string($rawColor) || !is_string($rawSport)) {
        http_response_code(422);
        $error = 'Enter one text value for color and one for sport.';
    } else {
        $color = trim($rawColor);
        $sport = trim($rawSport);
        $validColor = $color !== '' && strlen($color) <= 120 && preg_match('//u', $color) === 1;
        $validSport = $sport !== '' && strlen($sport) <= 120 && preg_match('//u', $sport) === 1;
        if (!$validColor || !$validSport) {
            $color = $validColor ? $color : '';
            $sport = $validSport ? $sport : '';
            http_response_code(422);
            $error = 'Both values must be valid UTF-8 and 1–120 bytes long.';
        }
    }
}
function e(string $value): string {
    return htmlspecialchars($value, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
}
?>
<!doctype html>
<html lang="en">
<head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>POST form preview</title></head>
<body>
<form method="post" action="post-form.php">
  <p><label>Favorite color <input name="color" value="<?= e($color) ?>" required></label></p>
  <p><label>Favorite sport <input name="sport" value="<?= e($sport) ?>" required></label></p>
  <button type="submit">Preview</button>
</form>
<?php if ($error !== ''): ?>
<p><?= e($error) ?></p>
<?php elseif ($submitted): ?>
<p>Color: <?= e($color) ?> / Sport: <?= e($sport) ?></p>
<?php endif; ?>
</body>
</html>

?? supplies a fallback for a missing value. Array-shaped fields fail the string check and receive 422. POST values are trimmed and must be nonempty. strlen() counts bytes, which differ from character counts in UTF-8. Both examples allow up to 120 bytes and validate UTF-8 with preg_match('//u', …). HTML required does not replace server-side validation.

Diagnose missing fields and body formats

If $_POST is empty, check the case of the field name, request method, and Content-Type. PHP reads fields from application/x-www-form-urlencoded or multipart/form-data. File uploads use $_FILES. A separate JSON endpoint reads and parses php://input and validates the parsed values. For large forms check post_max_size and max_input_vars. PHP Manual · $_FILES PHP Manual · Core php.ini Directives

The preview stores nothing. A real registration form needs authorization, protection against cross-site request forgery (CSRF), and duplicate-submission handling. A 303 redirect to a GET page is an option after saving.

Display values safely in HTML

Enter blue & "green" and verify that the same text appears in the result and the input. htmlspecialchars() uses ENT_QUOTES and UTF-8 to encode quotes and other special characters for HTML. Apply it at output. Bind validated values as SQL parameters and handle JavaScript and URL values according to their own contexts. PHP Manual · htmlspecialchars()