User Registration
Key Techniques Used
- Self-posting form:
action=""posts to same script - Field preservation:
value="<?= htmlspecialchars($field) ?>" - Per-field errors: Check
$errors['field']existence - Checkbox handling:
isset($_POST['name'])(only sent if checked)
The Validation Pattern
<?php
$errors = [];
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$username = trim($_POST['username'] ?? '');
// Validation rules
if (empty($username)) {
$errors['username'] = 'Username is required';
} elseif (strlen($username) < 3) {
$errors['username'] = 'Username must be at least 3 characters';
}
// Process only if no errors
if (empty($errors)) {
// Save to database, send email, etc.
header('Location: success.php');
exit;
}
}
?>
Previous: POST Example | Back to Module | Next: Headers & Metadata