Every website that collects information from a user — a login page, a contact form, a search bar — relies on one core skill: reading data submitted by that user. PHP Forms make this possible by connecting HTML input fields to PHP code that can process the submitted values.
This is Tutorial 9 of 10 in our PHP Basics for Beginners series. In this tutorial, you will learn how PHP Forms work together with superglobal variables to collect and process real user input.
What are PHP Forms?
PHP Forms combine a standard HTML form with PHP code that captures whatever the user types or selects. When a user submits a form, the browser sends that data to a specified PHP file, which then reads the values using special built-in variables called superglobals.
What are Superglobals?
Superglobals are built-in PHP variables that are always accessible, no matter where you are in your script — inside a function, outside a function, or anywhere else. When working with PHP Forms, the two most important superglobals you will use are $_GET and $_POST.
The $_GET Superglobal
The $_GET superglobal collects data sent through the URL, usually when a form uses the GET method or when parameters are added directly to a link. Data sent through $_GET becomes visible in the browser’s address bar.
<form action="welcome.php" method="GET">
Name: <input type="text" name="username">
<input type="submit" value="Submit">
</form>
On the receiving page, you access this value using PHP Forms syntax like this:
<?php
$name = $_GET["username"];
echo "Hello, " . $name;
?>
Because $_GET data appears in the URL, it works well for non-sensitive information like search queries or filters, but it is not suitable for passwords or private data.
The $_POST Superglobal
The $_POST superglobal collects data sent through the request body instead of the URL, making it the preferred choice for PHP Forms that handle sensitive or lengthy information like login credentials or registration details.
<form action="welcome.php" method="POST">
Email: <input type="text" name="email">
<input type="submit" value="Submit">
</form>
<?php
$email = $_POST["email"];
echo "Your email is: " . $email;
?>
Since $_POST data does not appear in the URL, it keeps submitted values hidden from the browser’s address bar, making PHP Forms built with the POST method noticeably more private than GET-based forms.
The $_REQUEST Superglobal
PHP also provides a $_REQUEST superglobal, which can collect data regardless of whether the form used GET or POST as its method. While convenient, most developers prefer sticking to $_GET or $_POST directly, since $_REQUEST can sometimes create confusion about where the data actually came from.
<?php
$value = $_REQUEST["username"];
echo $value;
?>
Checking if a Form was Submitted
In real PHP Forms, you rarely want code to run before the user actually submits anything. The isset() function checks whether a specific value exists, allowing you to safely wrap your form-processing logic.
<?php
if (isset($_POST["email"])) {
$email = $_POST["email"];
echo "Your email is: " . $email;
}
?>
Wrapping PHP Forms logic inside an isset() check prevents PHP from throwing warnings when the page loads for the first time, before any submission has occurred.
Basic Input Validation
Raw user input should never be trusted blindly. Combining PHP Forms with the string functions you learned in the previous tutorial helps you catch empty or improperly formatted values before processing them further.
<?php
if (isset($_POST["username"])) {
$username = trim($_POST["username"]);
if (empty($username)) {
echo "Username cannot be empty.";
} else {
echo "Welcome, " . $username;
}
}
?>
Using trim() removes accidental spaces, while empty() confirms the field actually contains a value before your PHP Forms logic proceeds further. For advanced security practices around handling user input, refer to the official PHP security guide.
Building a Complete Simple Form
Let’s combine everything into one complete example. Here is an HTML form paired with PHP code that processes the submission on the same page.
<?php
if (isset($_POST["fullname"])) {
$fullname = trim($_POST["fullname"]);
echo "Thank you, " . $fullname . "! Your form was submitted.";
}
?>
<form action="" method="POST">
Full Name: <input type="text" name="fullname">
<input type="submit" value="Submit">
</form>
This pattern, where a PHP Forms script processes and displays its own form on a single page, is extremely common in real beginner and intermediate projects alike.
What’s Next?
You now understand how PHP Forms work using $_GET, $_POST, $_REQUEST, and basic validation with isset() and empty(). In the next tutorial, we will explore PHP Sessions and Cookies, which let your application remember users across multiple page visits.
If you missed the previous lesson, check out Tutorial 8: PHP String Functions, or explore all lessons in our PHP category.
Practice Exercise
Complete the following tasks to reinforce what you learned in this tutorial:
- Create a PHP file named
forms-practice.php - Build an HTML form with fields for name and age, using the POST method
- Write PHP code that checks if the form was submitted using isset()
- Use trim() to clean the name input before displaying it back to the user
- Use empty() to check if the age field was left blank, and show an error message if so
- Add a second form using the GET method that accepts a search keyword and displays it on submission
- Print the full contents of $_POST using print_r() to see the raw submitted data structure
Bonus Challenge: Build a simple feedback form with name, email, and message fields, then validate that all three fields are filled before displaying a “Thank you for your feedback” message using PHP Forms logic.

