Introduction
GET and POST Methods in PHP are two important HTTP methods used to send form data from an HTML page to a PHP script. GET sends data through the URL, while POST sends data through the request body. In this chapter, you will practise both methods through practical PHP examples, including search forms, login forms, student registration, and employee data submission.
Question 1: Submit a Form Using the GET Method
Problem Statement
Create an HTML form that accepts a student’s name and displays it using PHP’s $_GET method.
PHP Solution
index.php
<!DOCTYPE html>
<html>
<body>
<form method="GET" action="">
Enter Student Name:
<input type="text" name="student">
<button type="submit">Submit</button>
</form>
<?php
if (isset($_GET["student"])) {
echo "Student Name: " . htmlspecialchars($_GET["student"]);
}
?>
</body>
</html>
Sample Output
Enter Student Name: Rahul
Student Name: Rahul
Explanation
method="GET"sends form data through the URL.name="student"identifies the input field.$_GET["student"]retrieves the submitted value.htmlspecialchars()helps prevent HTML or script injection when displaying user input.
Concepts Covered
- GET method
$_GETsuperglobal- HTML forms
- Displaying submitted data
Question 2: Submit a Form Using the POST Method
Problem Statement
Create a form that accepts an employee’s name and displays it using the POST method.
PHP Solution
index.php
<!DOCTYPE html>
<html>
<body>
<form method="POST" action="">
Enter Employee Name:
<input type="text" name="employee">
<button type="submit">Submit</button>
</form>
<?php
if (isset($_POST["employee"])) {
echo "Employee Name: " . htmlspecialchars($_POST["employee"]);
}
?>
</body>
</html>
Sample Output
Enter Employee Name: Amit
Employee Name: Amit
Explanation
method="POST"sends data in the HTTP request body.$_POST["employee"]retrieves the submitted employee name.- Unlike GET, the submitted value does not normally appear in the URL.
Concepts Covered
- POST method
$_POSTsuperglobal- Form submission
- Request body
Question 3: Create a GET-Based Search Form
Problem Statement
Create a search form that accepts a product name and displays the searched product using GET.
PHP Solution
<!DOCTYPE html>
<html>
<body>
<form method="GET" action="">
Search Product:
<input type="text" name="product">
<button type="submit">Search</button>
</form>
<?php
if (isset($_GET["product"])) {
$product = trim($_GET["product"]);
if ($product !== "") {
echo "You searched for: " . htmlspecialchars($product);
} else {
echo "Please enter a product name.";
}
}
?>
</body>
</html>
Sample Output
Search Product: Laptop
You searched for: Laptop
Explanation
- GET is useful for search forms because search values can appear in the URL.
trim()removes unnecessary spaces from the beginning and end.- The condition checks whether the user entered a value.
htmlspecialchars()safely displays the search text.
Concepts Covered
- GET-based search
- Retrieving URL parameters
- Input checking
- Displaying search results
Question 4: Create a POST-Based Login Form
Problem Statement
Create a basic login form that accepts a username and password using POST and checks them against sample credentials.
PHP Solution
<!DOCTYPE html>
<html>
<body>
<form method="POST" action="">
Username:
<input type="text" name="username">
<br><br>
Password:
<input type="password" name="password">
<br><br>
<button type="submit">Login</button>
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] === "POST") {
$username = $_POST["username"] ?? "";
$password = $_POST["password"] ?? "";
if ($username === "admin" && $password === "12345") {
echo "Login Successful!";
} else {
echo "Invalid Username or Password.";
}
}
?>
</body>
</html>
Sample Output
Input:
Username: admin
Password: 12345
Output:
Login Successful!
Explanation
$_SERVER["REQUEST_METHOD"]identifies the HTTP request method.$_POSTretrieves the submitted username and password.- The
&&operator checks whether both values match. - This is only a learning example. Real login systems should use securely stored password hashes with
password_verify(), HTTPS, and appropriate security protections.
Concepts Covered
- POST login form
- Request method checking
- Username and password validation
- Conditional statements
Question 5: Submit Multiple Student Details Using POST
Problem Statement
Create a student form that accepts name, email, and course, then displays all submitted information.
PHP Solution
<!DOCTYPE html>
<html>
<body>
<form method="POST" action="">
Student Name:
<input type="text" name="name">
<br><br>
Email:
<input type="email" name="email">
<br><br>
Course:
<input type="text" name="course">
<br><br>
<button type="submit">Register</button>
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] === "POST") {
$name = trim($_POST["name"] ?? "");
$email = trim($_POST["email"] ?? "");
$course = trim($_POST["course"] ?? "");
echo "<h3>Student Details</h3>";
echo "Name: " . htmlspecialchars($name) . "<br>";
echo "Email: " . htmlspecialchars($email) . "<br>";
echo "Course: " . htmlspecialchars($course);
}
?>
</body>
</html>
Sample Output
Student Details
Name: Rahul Sharma
Email: rahul@gmail.com
Course: PHP Development
Explanation
- The form collects three different values.
- Each input has a unique
nameattribute. - PHP retrieves each value through
$_POST. htmlspecialchars()protects the displayed output.
Concepts Covered
- Multiple POST parameters
- Form data collection
- Displaying multiple values
- Basic registration form
Question 6: Validate Form Data Using POST
Problem Statement
Create a registration form that checks whether the user’s name and email address are valid before displaying a success message.
PHP Solution
<!DOCTYPE html>
<html>
<body>
<form method="POST" action="">
Name:
<input type="text" name="name">
<br><br>
Email:
<input type="text" name="email">
<br><br>
<button type="submit">Register</button>
</form>
l>
<?php
if ($_SERVER["REQUEST_METHOD"] === "POST") {
$name = trim($_POST["name"] ?? "");
$email = trim($_POST["email"] ?? "");
if ($name === "") {
echo "Name is required.";
} elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "Enter a valid email address.";
} else {
echo "Registration Successful!";
}
}
?>
</body>
</htm
Sample Output
Valid Input:
Name: Rahul
Email: rahul@gmail.com
Output:
Registration Successful!
Invalid Input:
Name: Rahul
Email: rahul123
Output:
Enter a valid email address.
Explanation
- PHP checks whether the name is empty.
filter_var()withFILTER_VALIDATE_EMAILchecks the email format.- The success message appears only when both validations pass.
Concepts Covered
- POST data validation
- Required field checking
- Email validation
filter_var()
Question 7: Pass Data Between Two PHP Pages Using GET
Problem Statement
Create two PHP files. The first file accepts a product name and price. The second file receives and displays these values through GET.
PHP Solution
product.php
<!DOCTYPE html>
<html>
<body>
<form method="GET" action="details.php">
Product Name:
<input type="text" name="product">
<br><br>
Price:
<input type="number" name="price">
<br><br>
<button type="submit">View Details</button>
</form>
</body>
</html>
details.php
<?php
$product = $_GET["product"] ?? "";
$price = $_GET["price"] ?? "";
echo "Product Name: " . htmlspecialchars($product) . "<br>";
if (is_numeric($price) && $price !== "") {
echo "Product Price: ₹" . htmlspecialchars($price);
} else {
echo "Invalid product price.";
}
?>
Sample Output
Input:
Product Name: Keyboard
Price: 1200
Output on details.php:
Product Name: Keyboard
Product Price: ₹1200
Explanation
action="details.php"sends the form data to another PHP page.- GET places submitted values in the URL.
$_GETretrieves those values on the receiving page.- The price is checked before displaying it.
Concepts Covered
- GET between PHP pages
- Form action
- Receiving URL parameters
- Data validation
Question 8: Submit Employee Salary Details Using POST
Problem Statement
Create an employee salary form that accepts basic salary and allowance. Calculate and display the gross salary using POST.
PHP Solution
<!DOCTYPE html>
<html>
<body>
<form method="POST" action="">
Employee Name:
<input type="text" name="name">
<br><br>
Basic Salary:
<input type="number" name="basic" step="0.01">
<br><br>
Allowance:
<input type="number" name="allowance" step="0.01">
<br><br>
<button type="submit">Calculate Salary</button>
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] === "POST") {
$name = trim($_POST["name"] ?? "");
$basic = $_POST["basic"] ?? "";
$allowance = $_POST["allowance"] ?? "";
if (
$name === "" ||
$basic === "" ||
$allowance === "" ||
!is_numeric($basic) ||
!is_numeric($allowance) ||
(float)$basic < 0 ||
(float)$allowance < 0
) {
echo "Please enter valid employee details.";
} else {
$grossSalary = (float)$basic + (float)$allowance;
echo "Employee: " . htmlspecialchars($name) . "<br>";
echo "Gross Salary: ₹" . number_format($grossSalary, 2);
}
}
?>
</body>
</html>
Sample Output
Employee: Amit
Gross Salary: ₹35,000.00
Example Input:
Basic Salary: 30000
Allowance: 5000
Explanation
- The form sends employee details using POST.
- PHP checks that the salary values are numeric and non-negative.
- Gross salary is calculated by adding basic salary and allowance.
number_format()displays the amount with two decimal places.
Concepts Covered
- POST salary form
- Numeric validation
- Salary calculation
- Currency formatting
Question 9: Compare GET and POST Request Methods
Problem Statement
Create a PHP program that accepts a value using GET or POST and displays which request method was used.
PHP Solution
<!DOCTYPE html>
<html>
<body>
<h3>GET Method</h3>
<form method="GET" action="">
<input type="text" name="get_value">
<button type="submit">Send Using GET</button>
</form>
<h3>POST Method</h3>
<form method="POST" action="">
<input type="text" name="post_value">
<button type="submit">Send Using POST</button>
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] === "GET" && isset($_GET["get_value"])) {
echo "Request Method: GET<br>";
echo "Submitted Value: " . htmlspecialchars($_GET["get_value"]);
} elseif ($_SERVER["REQUEST_METHOD"] === "POST") {
echo "Request Method: POST<br>";
echo "Submitted Value: " . htmlspecialchars($_POST["post_value"] ?? "");
}
?>
</body>
</html>
Sample Output
When GET is submitted:
Request Method: GET
Submitted Value: Hello PHP
When POST is submitted:
Request Method: POST
Submitted Value: Hello PHP
Explanation
$_SERVER["REQUEST_METHOD"]tells PHP which HTTP method was used.- GET values are retrieved through
$_GET. - POST values are retrieved through
$_POST. - The same PHP page can process different request methods.
Concepts Covered
- HTTP request methods
- GET and POST comparison
$_SERVER- Request method detection
Question 10: Build a Complete Student Registration Form Using POST
Problem Statement
Create a student registration form that accepts name, email, phone number, gender, and course. Validate the submitted details and display a registration summary.
PHP Solution
<!DOCTYPE html>
<html>
<body>
<h2>Student Registration</h2>
<form method="POST" action="">
Name:
<input type="text" name="name">
<br><br>
Email:
<input type="text" name="email">
<br><br>
Phone:
<input type="text" name="phone">
<br><br>
Gender:
<select name="gender">
<option value="">Select Gender</option>
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
<br><br>
Course:
<select name="course">
<option value="">Select Course</option>
<option value="PHP">PHP</option>
<option value="Python">Python</option>
<option value="Java">Java</option>
<option value="Web Development">Web Development</option>
</select>
<br><br>
<button type="submit">Register</button>
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] === "POST") {
$name = trim($_POST["name"] ?? "");
$email = trim($_POST["email"] ?? "");
$phone = trim($_POST["phone"] ?? "");
$gender = $_POST["gender"] ?? "";
$course = $_POST["course"] ?? "";
$allowedGenders = ["Male", "Female"];
$allowedCourses = [
"PHP",
"Python",
"Java",
"Web Development"
];
if ($name === "") {
echo "Name is required.";
} elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "Enter a valid email.";
} elseif (!preg_match("/^[0-9]{10}$/", $phone)) {
echo "Enter a valid 10-digit phone number.";
} elseif (!in_array($gender, $allowedGenders, true)) {
echo "Select a valid gender.";
} elseif (!in_array($course, $allowedCourses, true)) {
echo "Select a valid course.";
} else {
echo "<h3>Registration Successful!</h3>";
echo "Name: " . htmlspecialchars($name) . "<br>";
echo "Email: " . htmlspecialchars($email) . "<br>";
echo "Phone: " . htmlspecialchars($phone) . "<br>";
echo "Gender: " . htmlspecialchars($gender) . "<br>";
echo "Course: " . htmlspecialchars($course);
}
}
?>
</body>
</html>
Sample Output
Registration Successful!
Name: Rahul Sharma
Email: rahul@gmail.com
Phone: 9876543210
Gender: Male
Course: PHP
Explanation
- The form collects five student details using POST.
- PHP validates the name, email, and phone number.
preg_match()checks whether the phone number contains exactly 10 digits.in_array()ensures that the selected gender and course belong to the allowed options.- The registration summary appears only when all validations pass.
Concepts Covered
- Complete POST registration form
- Multiple form fields
- Email and phone validation
- Dropdown validation
- Registration summary
Key Takeaways
- GET and POST are HTTP methods used to send data to PHP.
- GET sends parameters through the URL.
- POST sends data through the request body.
$_GETretrieves GET parameters.$_POSTretrieves POST parameters.$_SERVER["REQUEST_METHOD"]identifies the request method.- GET is commonly used for searches and shareable filters.
- POST is commonly used for registration and data submission.
- Neither method automatically makes submitted data secure.
- Always validate incoming data and safely escape output.
Frequently Asked Questions (FAQs)
1. What is the GET method in PHP?
GET is an HTTP method that sends form data through URL parameters. PHP retrieves this data using the $_GET superglobal.
2. What is the POST method in PHP?
POST is an HTTP method that sends form data in the request body. PHP retrieves the submitted information using $_POST.
3. What is the main difference between GET and POST?
GET sends data through the URL, while POST sends data through the request body. GET is commonly used for searches, whereas POST is commonly used for submitting forms.
4. Which is more secure: GET or POST?
POST keeps submitted values out of the URL, but it does not encrypt the data. Use HTTPS and proper validation and security controls when handling sensitive information.
5. Can we send data from one PHP page to another using GET?
Yes. Set the form’s action attribute to the receiving PHP file and use $_GET to retrieve the submitted values.
6. Can we use GET and POST on the same PHP page?
Yes. PHP can identify the incoming method using $_SERVER["REQUEST_METHOD"] and process the appropriate data.
7. Why should we avoid sending passwords through GET?
GET places submitted values in the URL, which may appear in browser history, logs, and shared links. POST over HTTPS is more appropriate for submitting passwords.
8. What is the $_REQUEST variable in PHP?
$_REQUEST is a PHP superglobal that can contain data from GET, POST, and COOKIE requests, depending on PHP configuration. Using $_GET or $_POST directly is usually clearer.
9. Can GET and POST send multiple form values?
Yes. Both methods can send multiple form fields. Each field should have a name attribute so PHP can identify its value.
10. Does POST data appear in the browser URL?
No. POST data is normally sent in the HTTP request body, so it does not appear as URL query parameters.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
