Skip to content

Form Submission: A Step-by-Step Guide

  • HTML forms are used to collect user input and send it to a server for handling.
  • Forms are essential for user registration, login systems, contact forms, and any interactive web functionality.

Understanding the journey from browser to server is important for effective form handling:

  1. User fills out form → Data exists only in browser
  2. User clicks submit → Browser packages data
  3. Data travels to server → Via HTTP request
  4. PHP receives data → Through superglobal arrays
  5. PHP processes data → Validation, storage, response
  6. Server sends response → Back to user’s browser

  • Data sent in URL parameters
  • Visible in browser address bar
  • Limited data size (~2048 characters)
  • Use for: Search forms, filtering, shareable URLs
  • Data sent in request body
  • Not visible in URL
  • No size limitations
  • Use for: Sensitive data (passwords, personal info), large forms

PHP provides built-in arrays that automatically capture form data regardless of scope.

  • $_POST: Data from POST method forms
  • $_GET: Data from GET method forms
  • $_REQUEST: Combined POST, GET, and COOKIE data (less secure, avoid when possible)

Set up the user interface that collects information from users.

<form method="POST" action="process.php">
<label for="username">Username:</label>
<input type="text" name="username" id="username" required>
<label for="email">Email:</label>
<input type="email" name="email" id="email" required>
<button type="submit">Submit</button>
</form>

Create the server-side script that receives and handles form data. Always check if the form was actually submitted.

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Form was submitted - safe to process $_POST data
}

Use the form field’s name attribute to access submitted data.

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Use null coalescing for safe retrieval
$username = $_POST['username'] ?? '';
$email = $_POST['email'] ?? '';
}

Ensure the data meets your requirements before processing. Never trust user input directly.

$errors = [];
if (empty($username)) {
$errors[] = 'Username is required';
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors[] = 'Valid email is required';
}
if (empty($errors)) {
// Data is valid - proceed with processing
}

Remove potentially harmful content while preserving legitimate data.

$username = trim($_POST['username']);
$username = htmlspecialchars($username, ENT_QUOTES, 'UTF-8');
$email = trim($_POST['email']);
$email = filter_var($email, FILTER_SANITIZE_EMAIL);

Perform the actual work with the validated, sanitized data. At this point, the data is safe to pass to your database.

if (empty($errors)) {
// Data is validated and sanitized - safe to use in database queries
// For a registration form (INSERT):
$stmt = $pdo->prepare("INSERT INTO users (username, email) VALUES (?, ?)");
$stmt->execute([$username, $email]);
// For a profile update form (UPDATE):
// $stmt = $pdo->prepare("UPDATE users SET email = ? WHERE id = ?");
// $stmt->execute([$email, $user_id]);
$success_message = "Thank you, " . htmlspecialchars($username) . "!";
}

Provide appropriate feedback to users based on processing results.

if (!empty($errors)) {
foreach ($errors as $error) {
echo "<p class='error'>" . htmlspecialchars($error) . "</p>";
}
}

Redirect after successful form processing to prevent duplicate submissions when users refresh the page.

if ($_SERVER['REQUEST_METHOD'] === 'POST' && empty($errors)) {
// Process the form data
// Save to database, send email, etc.
// Store success message in session
$_SESSION['message'] = 'Form submitted successfully!';
// Redirect to prevent resubmission
header('Location: success.php');
exit();
}

Never redirect when validation fails. Keep the user on the same page to:

  • Display validation errors
  • Preserve user input data
  • Allow immediate correction
  • Valid Data: POST → Process → Redirect → GET → Show Success
  • Invalid Data: POST → Validate → Stay on Page → Show Errors

  • Use HTTPS for sensitive data transmission
  • Implement CSRF tokens for forms that modify data
  • Use prepared statements for database queries (prevents SQL injection)

When forms aren’t working:

  1. Check submission data: var_dump($_POST)
  2. Verify field names match between HTML and PHP
  3. Review server error logs for PHP errors
  4. Use browser dev tools to inspect network requests
  • Mismatched field names between HTML and PHP
  • Missing form method or action attributes
  • Output sent before header() redirects
  • Case sensitivity in field names