PHP Form Handling
Forms are how visitors send data to a PHP application: a search box, a login screen, a checkout page. The browser collects what the user typed, packs it into an HTTP request and sends it to a PHP script. PHP then unpacks that data into two special arrays, $_GET and $_POST, so your code can read it like any other array.
A simple HTML form
Here is a small newsletter sign-up form. The action attribute names the script that receives the data, and method chooses how it is sent:
<form action="signup.php" method="post">
<label>Name: <input type="text" name="name"></label>
<label>Email: <input type="email" name="email"></label>
<label>Topic:
<select name="topic">
<option value="php">PHP</option>
<option value="sql">SQL</option>
</select>
</label>
<button type="submit">Subscribe</button>
</form>The name attribute of each field becomes the key in the PHP array. A field without a name is never sent.
How the data arrives
When the visitor presses Subscribe, the browser sends something like this to the server:
POST /signup.php HTTP/1.1
Host: example.test
Content-Type: application/x-www-form-urlencoded
name=Mai+Tran&email=mai%40example.test&topic=phpPHP decodes the body (+ becomes a space, %40 becomes @) and fills $_POST:
$_POST = ['name' => 'Mai Tran', 'email' => 'mai@example.test', 'topic' => 'php'];The playgrounds on this site run PHP as a command-line script, so no browser submits anything. To practise, we fill the superglobals ourselves at the top of each example — the rest of the code is exactly what you would write on a real server.
<?php
// Simulate the request the browser would send
$_SERVER['REQUEST_METHOD'] = 'POST';
$_POST = ['name' => 'Mai Tran', 'email' => 'mai@example.test', 'topic' => 'php'];
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$name = $_POST['name'] ?? '';
$topic = $_POST['topic'] ?? 'none';
echo "Thanks, $name!" . PHP_EOL;
echo "You subscribed to the $topic newsletter." . PHP_EOL;
}Thanks, Mai Tran!
You subscribed to the php newsletter.
Checking $_SERVER['REQUEST_METHOD'] lets one script both show the form (on a normal GET visit) and process it (on POST). The null coalescing operator ?? supplies a default when a field is missing, which avoids "undefined array key" warnings.
GET vs POST
With method="get" the fields are appended to the URL as a query string, for example search.php?q=arrays&page=2, and PHP puts them in $_GET.
| GET | POST | |
|---|---|---|
| Where data travels | In the URL (?key=value&...) |
In the request body |
| Visible in history/bookmarks | Yes | No |
| Size limit | Small (URL length limits) | Large (set by post_max_size) |
| Good for | Searches, filters, pagination | Logins, sign-ups, anything that changes data |
| PHP array | $_GET |
$_POST |
<?php
// Simulate a visit to: search.php?q=arrays&page=2
$_SERVER['REQUEST_METHOD'] = 'GET';
$_GET = ['q' => 'arrays', 'page' => '2'];
$query = $_GET['q'] ?? '';
$page = (int) ($_GET['page'] ?? 1);
echo "Searching for \"$query\", page $page" . PHP_EOL;
var_dump($_GET['page']);
var_dump($page);Searching for "arrays", page 2
string(1) "2"
int(2)
Note: Everything that arrives from a form is a string (or an array of strings), even numbers. Cast or validate values before you calculate with them.
Never echo raw input
Whatever the user types ends up in your page if you print it. If someone enters <script>...</script> as their name and you echo it unchanged, the browser runs that script — an attack called cross-site scripting (XSS). Always escape output with htmlspecialchars():
<?php
$_POST = ['name' => '<b>Bob</b> & <script>alert(1)</script>'];
$raw = $_POST['name'];
$safe = htmlspecialchars($raw, ENT_QUOTES, 'UTF-8');
echo "Unsafe: $raw" . PHP_EOL;
echo "Safe: $safe" . PHP_EOL;Unsafe: <b>Bob</b> & <script>alert(1)</script>
Safe: <b>Bob</b> & <script>alert(1)</script>
The escaped version is displayed by the browser as literal text instead of being interpreted as HTML.
Checkboxes and multiple values
When several inputs share a name ending in [], PHP collects them into an array:
<input type="checkbox" name="langs[]" value="php"> PHP
<input type="checkbox" name="langs[]" value="js"> JavaScript
<input type="checkbox" name="langs[]" value="sql"> SQLUnchecked boxes are simply not sent, so the key may be missing entirely:
<?php
$_POST = ['langs' => ['php', 'sql']];
$langs = $_POST['langs'] ?? [];
echo "Selected: " . count($langs) . PHP_EOL;
foreach ($langs as $lang) {
echo "- " . htmlspecialchars($lang) . PHP_EOL;
}Selected: 2
- php
- sql
Tip:
$_REQUESTmerges$_GET,$_POSTand cookies. It is convenient but ambiguous — prefer the specific array so you know where the data came from.
Test yourself
Which attribute of an <input> decides its key in $_POST?
idnamevaluetype
A form uses method="get". Where does PHP put the submitted fields?
$_POST$_SERVER['BODY']$_GET$_FORM
Why should user input be passed through htmlspecialchars() before echoing it?
- To make it shorter
- To convert it to a number
- To prevent the browser from running injected HTML/JavaScript
- To store it in a session