Web Development Lesson 12 of 17
PHP Forms — GET, POST and htmlspecialchars
Receive form data in PHP with $_GET and $_POST, know when to use each method, display user input safely with htmlspecialchars, and handle a form on the same page.
Forms are how users send data to your site: logins, registrations, orders, comments. In PHP, form data arrives in two special arrays: $_GET and $_POST.
A simple form
greet.html:
<form action="greet.php" method="post">
<label>Name: <input name="name"></label>
<label>Section: <input name="section"></label>
<button>Submit</button>
</form>
greet.php:
<?php
$name = $_POST["name"] ?? "";
$section = $_POST["section"] ?? "";
echo "<p>Hello, " . htmlspecialchars($name) . " of " . htmlspecialchars($section) . "!</p>";
If the user types Ana and Mabini
<p>Hello, Ana of Mabini!</p>
- Each input's
nameattribute becomes a key in$_POST. ?? ""gives an empty string if the field is missing.htmlspecialcharsmakes the text safe to show (explained below).
GET vs POST
| GET | POST | |
|---|---|---|
| Data appears in | The URL: search.php?q=kape | The request body (hidden from the URL) |
| Can be bookmarked/shared | Yes | No |
| Use for | Searching, filtering, page numbers | Logins, registrations, anything that changes data |
| PHP array | $_GET | $_POST |
A GET search
<?php
$products = ["Pandesal", "Kape 3-in-1", "Kape Barako", "Itlog"];
$q = trim($_GET["q"] ?? "");
$results = array_filter($products, fn($p) => $q === "" || stripos($p, $q) !== false);
?>
<form method="get">
<input name="q" value="<?= htmlspecialchars($q) ?>" placeholder="Search">
<button>Search</button>
</form>
<ul>
<?php foreach ($results as $p): ?>
<li><?= htmlspecialchars($p) ?></li>
<?php endforeach; ?>
</ul>
Visiting search.php?q=kape lists "Kape 3-in-1" and "Kape Barako". stripos searches without caring about capital letters.
XSS: why htmlspecialchars matters
Suppose a user types this as their name:
<script>alert("Hacked!")</script>
If you echo $_POST["name"] directly, that script runs in the browser of everyone who views the page. This attack is called cross-site scripting (XSS). htmlspecialchars turns special characters into harmless entities:
<?php
$name = '<script>alert("Hacked!")</script>';
echo htmlspecialchars($name);
Output
<script>alert("Hacked!")</script>
The browser shows the text literally instead of running it.
Form and handler on one page
A common pattern: the page shows the form, and when it's submitted, processes it:
<?php
$message = "";
if ($_SERVER["REQUEST_METHOD"] === "POST") {
$qty = (int) ($_POST["qty"] ?? 0);
$message = "Total for $qty pandesal: ₱" . number_format($qty * 2.5, 2);
}
?>
<form method="post">
<input type="number" name="qty" min="1" required>
<button>Compute</button>
</form>
<p><?= htmlspecialchars($message) ?></p>
$_SERVER["REQUEST_METHOD"] tells you whether the page was loaded normally (GET) or by submitting the form (POST).
Checkboxes, radio buttons and selects
<form method="post">
<select name="strand">
<option>STEM</option><option>ABM</option><option>HUMSS</option>
</select>
<label><input type="radio" name="shift" value="am"> Morning</label>
<label><input type="radio" name="shift" value="pm"> Afternoon</label>
<label><input type="checkbox" name="clubs[]" value="coding"> Coding</label>
<label><input type="checkbox" name="clubs[]" value="chess"> Chess</label>
<button>Save</button>
</form>
<?php
$strand = $_POST["strand"] ?? "";
$shift = $_POST["shift"] ?? "none";
$clubs = $_POST["clubs"] ?? []; // array, thanks to name="clubs[]"
echo "Strand: $strand, shift: $shift, clubs: " . implode(", ", $clubs);
An unchecked checkbox sends nothing at all, so always use ?? with a default.
Quick Quiz
Check what you learned. Walang grade ito, practice lang. 🙂
By Sir Reyel · Last updated 2026-12-04