POST Method

Introduction

The POST method in PHP is used to send data from an HTML form to the server securely. Unlike the GET method, POST sends data in the HTTP request body, which means the information is not visible in the browser URL.

The POST method is commonly used for login forms, registration forms, contact forms, and sensitive data submission.

Objectives

By the end of this training, you will be able to:

  • Understand the POST method in PHP
  • Create HTML forms using POST
  • Receive form data in PHP
  • Process user input securely
  • Differentiate between GET and POST methods
  • Build form-based web applications

What is the POST Method

The POST method transfers form data to the server through the request body. It is more secure than GET because data is hidden from the URL.

POST is suitable for:

  • Login forms
  • Registration systems
  • Password submission
  • Contact forms
  • Database operations

Features of POST Method

Data is Hidden

Form information does not appear in the browser address bar.

Supports Large Data

POST can send large amounts of information including text and files.

Better Security

Sensitive information like passwords is more secure with POST.

Used for Data Processing

POST is mainly used when inserting or updating data.

Basic HTML Form Using POST

<form method="post" action="process.php">
<input type="text" name="username" placeholder="Enter Name">
<input type="submit" value="Submit">
</form>

Receiving POST Data in PHP

<?php
$username = $_POST['username'];

echo $username;
?>

Complete POST Method Example

HTML Form

<!DOCTYPE html>
<html>
<head>
<title>POST Method</title>
</head>
<body>

<form method="post">
<input type="text" name="fullname" placeholder="Enter Full Name">
<input type="submit" value="Send">
</form>

</body>
</html>

PHP Processing

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {

$fullname = $_POST['fullname'];

echo "Welcome " . $fullname;
}
?>

How POST Method Works

  1. User fills out the form
  2. User clicks the submit button
  3. Browser sends data to the server
  4. PHP receives the data using $_POST
  5. Server processes and displays the result

Using Multiple Form Fields

<form method="post">
<input type="text" name="name" placeholder="Enter Name">
<input type="email" name="email" placeholder="Enter Email">
<input type="submit" value="Submit">
</form>
<?php
$name = $_POST['name'];
$email = $_POST['email'];

echo $name;
echo $email;
?>

Difference Between GET and POST

GET Method

  • Data appears in URL
  • Less secure
  • Limited data size
  • Used for searches and filters

POST Method

  • Data hidden from URL
  • More secure
  • Supports large data
  • Used for sensitive information

Checking Empty Fields

<?php
if (empty($_POST['username'])) {
echo "Name is required";
} else {
echo $_POST['username'];
}
?>

Preventing Errors with isset()

<?php
if (isset($_POST['username'])) {

$username = $_POST['username'];

echo $username;
}
?>

Form Validation Example

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {

if (!empty($_POST['email'])) {

$email = $_POST['email'];

echo "Email Submitted: " . $email;

} else {

echo "Please enter email";
}
}
?>

Advantages of POST Method

  • More secure than GET
  • Better for sensitive data
  • Handles large form submissions
  • Supports file uploading
  • Cleaner URLs

Real World Applications

The POST method is used in:

  • Login systems
  • Signup forms
  • Online shopping websites
  • Contact forms
  • Feedback systems
  • Database insertion forms

Best Practices

  • Always validate user input
  • Use isset() and empty() checks
  • Sanitize form data
  • Use POST for sensitive information
  • Combine POST with database security techniques

Final Presentation

In your final presentation, explain:

  • What the POST method is
  • How POST works in PHP
  • Difference between GET and POST
  • Benefits of using POST
  • Form handling examples
  • Real-world applications of POST method
Home Ā» Intermediate PHP > Forms Handling > POST Method