Table of Contents:
PHP Form Handling Tutorial (Complete Guide with Examples)
Welcome to phponline.in — your trusted resource to learn PHP programming from beginner to advanced.
In this tutorial, we’ll explore PHP Form Handling in depth, including:
- Introduction to form handling in PHP
- The role of
GET
andPOST
methods - Superglobals (
$_GET
,$_POST
,$_REQUEST
,$_SERVER
) - Form validation and sanitization
- Secure handling of user input
- Real-world examples (Login, Registration, Contact form)
- Preventing common vulnerabilities like XSS and SQL Injection
By the end, you’ll be able to:
- Create secure and efficient forms
- Collect and validate user data
- Integrate forms with databases (MySQL)
- Build mini projects like contact forms, login systems, and registration forms
You will Learn
- Introduction to PHP Form Handling
- Why Form Handling is Important
- HTML Forms Recap
- PHP Form Handling Workflow
- GET vs POST in Form Handling
- PHP Superglobals in Form Handling
- Handling Single Input Fields
- Handling Multiple Input Fields
- Handling Radio Buttons
- Handling Checkboxes
- Handling Select Dropdown
- Handling Textarea
- Handling Hidden Inputs
- Handling Password Inputs
- File Upload Handling in PHP Forms
- Form Handling using GET (Example)
- Form Handling using POST (Example)
- PHP Form Validation Basics
- Required Field Validation
- Email Validation
- Phone Number Validation
- Password Strength Validation
- Age and Number Validation
- Using filter_var() for Validation
- Regex in PHP Form Validation
- Sanitizing User Input
- htmlspecialchars() and htmlentities()
- trim(), stripslashes(), strip_tags()
- PHP Form Security Best Practices
- Preventing XSS in Forms
- Preventing SQL Injection in Forms
- Secure Database Insertion with Prepared Statements
- Sticky Forms (Preserve Data on Error)
- PHP Error Handling in Forms
- Multiple Page Form Handling
- Using PHP Sessions with Forms
- Using PHP Cookies with Forms
- Contact Form Example with PHP
- Login Form Example with PHP
- Registration Form Example with PHP & MySQL
- File Upload Form Example (Profile Picture)
- Image Upload Validation
- Storing Form Data in MySQL Database
- Retrieving and Displaying Stored Form Data
- PHP Form with AJAX (Basic Example)
- Secure Form Handling Checklist
- Common Mistakes in PHP Form Handling
- Best Practices in PHP Forms
- Mini Project: Feedback Form with Database Integration
- Mini Project: Newsletter Subscription Form with Email Confirmation
- Mini Project: Student Registration System with Validation & DB
1. Introduction to PHP Form Handling
Form handling in PHP refers to collecting, validating, processing, and storing user input submitted through HTML forms.
👉 Example: When a user fills out a login form, PHP processes their username and password to check if they are valid.
Forms + PHP = Dynamic Web Applications.
2. Why Form Handling is Important?
Forms are critical in web apps because they:
- Collect user data (login, registration, feedback)
- Enable interactivity
- Connect frontend (HTML) to backend (PHP & Database)
- Allow secure transactions
👉 Related: PHP Global Variables
5. GET vs POST in Form Handling
Feature | GET | POST |
---|---|---|
Data visible in URL | ✅ Yes | ❌ No |
Security | Less secure | More secure |
Data length limit | 2000 chars | Unlimited |
Use cases | Search forms, filters | Login, Registration, File Uploads |
👉 Related: PHP Superglobals
16. Form Handling using GET (Example)
<form method="get">
Name: <input type="text" name="name">
<input type="submit">
</form>
<?php
if (isset($_GET['name'])) {
echo "Hello, " . htmlspecialchars($_GET['name']);
}
?>
17. Form Handling using POST (Example)
<form method="post">
Email: <input type="text" name="email">
<input type="submit">
</form>
<?php
if (isset($_POST['email'])) {
echo "Your email: " . htmlspecialchars($_POST['email']);
}
?>
18. PHP Form Validation Basics
Validation ensures only correct and safe data is accepted.
if (empty($_POST["name"])) {
echo "Name is required!";
}
23. Age and Number Validation
$age = $_POST["age"];
if (!filter_var($age, FILTER_VALIDATE_INT)) {
echo "Invalid age!";
}
29. PHP Form Security Best Practices
- Always sanitize inputs
- Use
htmlspecialchars()
to prevent XSS - Use prepared statements to prevent SQL Injection
- Use CSRF tokens for high-security apps
👉 Related: [PHP Security Best Practices]
38. Contact Form Example
<form method="post">
Name: <input type="text" name="name"><br>
Email: <input type="text" name="email"><br>
Message: <textarea name="message"></textarea><br>
<input type="submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = htmlspecialchars($_POST['name']);
$email = htmlspecialchars($_POST['email']);
$message = htmlspecialchars($_POST['message']);
echo "Thanks, $name. We received your message.";
}
?>
39. Login Form Example
<form method="post">
Username: <input type="text" name="username"><br>
Password: <input type="password" name="password"><br>
<input type="submit" value="Login">
</form>
<?php
if ($_POST['username'] == "admin" && $_POST['password'] == "1234") {
echo "Login successful!";
} else {
echo "Invalid credentials!";
}
?>
👉 Related: PHP Operators
43. Storing Form Data in MySQL
$conn = mysqli_connect("localhost", "root", "", "testdb");
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = mysqli_real_escape_string($conn, $_POST['name']);
$email = mysqli_real_escape_string($conn, $_POST['email']);
$sql = "INSERT INTO users (name, email) VALUES ('$name', '$email')";
mysqli_query($conn, $sql);
}
👉 Related: PHP Arrays
Mini Project: Student Registration System
Features:
- Collects Name, Email, Phone, Password
- Validates & sanitizes inputs
- Stores securely in MySQL database
Summary & Conclusion
- PHP form handling is the core of web development
- Use POST for sensitive data
- Always validate & sanitize inputs
- Secure your forms with prepared statements and XSS protection
FAQs on PHP Form Handling
Q1: What is PHP Form Handling?
A: The process of collecting, validating, and processing data from HTML forms using PHP.
Q2: Which method is better for form handling – GET or POST?
A: POST is more secure and used for login, registration, and file uploads.
Q3: How do I validate a PHP form?
A: Use PHP functions like isset()
, empty()
, filter_var()
, and regex patterns.
Q4: How to secure PHP form handling?
A: Use prepared statements, sanitize input, and prevent XSS with htmlspecialchars()
.
Q5: Can PHP forms store data in a database?
A: Yes, PHP forms can connect to MySQL and store data using INSERT
queries.