Skip to content

PHP Tutorial Forms and User Input

Reading Form Data

Lesson 137 of 369 2 min read By Deepak Paswan Updated Oct 5, 2026

Each kind of form field arrives in PHP in a slightly different way.

Text, email, number, textarea

These arrive as strings, even when the type is number.

index.php
PHP
<?php
$_POST = ["name" => "Isha", "age" => "29", "bio" => "Loves PHP"];

$name = $_POST["name"] ?? "";
$age = $_POST["age"] ?? "";

var_dump($age);
Output
OUTPUT
string(2) "29"

Convert on purpose, after you validate.

Radio buttons

Only the selected option is sent. If nothing is chosen, the key is missing.

index.php
PHP
<?php
// <input type="radio" name="plan" value="basic"> ... value="pro">
$_POST = ["plan" => "pro"];

$plan = $_POST["plan"] ?? "basic";

echo "Plan: $plan";
Output
OUTPUT
Plan: pro

A single checkbox

An unchecked checkbox sends nothing. A checked one sends its value (or on if there is no value).

index.php
PHP
<?php
// <input type="checkbox" name="newsletter" value="yes">
$_POST = [];   // the box was left unchecked

$subscribe = isset($_POST["newsletter"]);

var_dump($subscribe);
Output
OUTPUT
bool(false)

Use isset(), not a comparison with a value.

Several checkboxes

Add [] to the name so PHP collects them in an array.

index.php
PHP
<?php
// <input type="checkbox" name="topics[]" value="php"> ... value="css">
$_POST = ["topics" => ["php", "css"]];

$topics = $_POST["topics"] ?? [];

echo "Topics: " . implode(", ", $topics);
Output
OUTPUT
Topics: php, css

Select lists

A <select> sends the value of the chosen option.

index.php
PHP
<?php
$_POST = ["country" => "IN"];

echo "Country code: " . ($_POST["country"] ?? "");
Output
OUTPUT
Country code: IN

A <select multiple name="skills[]"> sends an array, like several checkboxes.

Check against allowed values

The visitor can send any value, not only the ones in your <select>. Compare with your own list.

index.php
PHP
<?php
$allowed = ["IN", "US", "UK"];
$country = "XX";   // someone edited the request

if (!in_array($country, $allowed, true)) {
    echo "Please choose a valid country.";
}
Output
OUTPUT
Please choose a valid country.

Nested names

Names like user[name] and user[city] give you a nested array.

index.php
PHP
<?php
$_POST = ["user" => ["name" => "Isha", "city" => "Kochi"]];

echo $_POST["user"]["city"];
Output
OUTPUT
Kochi

A reading helper

A small function keeps the code tidy.

index.php
PHP
<?php
function post(string $key, string $default = ""): string
{
    $value = $_POST[$key] ?? $default;

    return is_string($value) ? trim($value) : $default;
}

$_POST = ["name" => "  Isha  "];

echo "[" . post("name") . "]";
Output
OUTPUT
[Isha]

The is_string() check matters: a visitor can send name[]=x, which makes $_POST["name"] an array and may break code that expects a string.

lightbulb
Plan for missing keys
Radios, checkboxes and multi-selects can be missing from the request. Always read with ?? or isset().