PHP seamlessly integrates with HTML forms to collect, process, and respond to user inputs. Form data is transmitted to server handlers using either the GET or POST HTTP methods.
GET vs POST Methodsflowchart TD
A["User Submits Form"] --> B{"Choose Form Method"}
B -- GET Method --> C["Appends params to URL (?search=php)"]
B -- POST Method --> D["Sends payload inside HTTP Request Body"]
C --> E["Visible in browser history; Ideal for Search & Filters"]
D --> F["Hidden from URL; Ideal for Passwords, Files, & Mutations"]
E & F --> G["Processed by PHP Server Handler"]
contact.html)<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Contact Form</title>
</head>
<body style="font-family: system-ui, sans-serif; background: #0f172a; color: #f8fafc; padding: 2rem;">
<h2>Contact Us</h2>
<form action="process.php" method="POST">
<label for="name">Your Name:</label><br>
<input type="text" id="name" name="fullname" required><br><br>
<label for="email">Your Email:</label><br>
<input type="email" id="email" name="email" required><br><br>
<button type="submit">Send Message</button>
</form>
</body>
</html>
process.php)<?php
declare(strict_types=1);
// Ensure request method is POST
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
die("Method Not Allowed: Please submit the form.");
}
// Extract and sanitize input variables
$fullname = htmlspecialchars(trim($_POST['fullname'] ?? ''));
$email = filter_var(trim($_POST['email'] ?? ''), FILTER_SANITIZE_EMAIL);
if (empty($fullname) || empty($email)) {
die("Validation Error: All fields are required.");
}
echo "<h1>Thank You, " . $fullname . "!</h1>";
echo "<p>We received your request. Confirmation sent to: " . htmlspecialchars($email) . "</p>";
POST for account creation, payments, and data deletion requests.GET forms allow users to bookmark search result URLs.$_SERVER['REQUEST_METHOD']: Verify request verb before processing form submission payloads.Create a PHP script search.php that reads a GET search parameter q using $_GET['q'] and echoes "Search results for: " sanitized with htmlspecialchars()!
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Experiment with the code from this lesson in our interactive playground.