PHP Form Validation: Alter a value in $_Post

This entry is part 5 of 7 in the series PHP Form Validation

If you want to use the whitespace-trimmed value subsequently in your program, alter the value in $_POST and the test the altered value.

$_POST['name'] = trim($_POST['name']);

if (strlen($_POST['name']) == 0) {
    $errors[] = 'Your name is required.';

Because $_POST is auto-global, a change to one of its elements inside the validate_form() function persists to other uses of $_POST after the change in another function, such as process_form().


This entry is part 16 of 54 in the series PHP Tutorial

$_POST is an associative array of variables passed to the current script via the HTTP POST method.

$_POST['name'] = "Michael";
echo 'Hello ' . htmlspecialchars($_POST['name']) . '!';

You can try to run the script here.

The output in the browser will be:

Hello Michael!

$_POST is mainly used in form processing. ┬áIt is an associative array and so we can pass the name Michael using $_POST[‘name’].