$_POST helps you get the information people type into a form on your website. It makes your site interactive by letting you use what users send.
0
0
$_POST for form submissions in PHP
Introduction
When you want to collect user login details like username and password.
When users fill out a contact form and you want to save or email their message.
When you have a survey or quiz and want to process the answers.
When users submit data that should not show in the website address bar.
When you want to handle form data securely and privately.
Syntax
PHP
<?php
$value = $_POST['field_name'];
?>Use the exact name attribute from your HTML form input inside the square brackets.
$_POST is an associative array that holds all form data sent by the POST method.
Examples
This gets the value typed in the input named 'name' and says hello.
PHP
<?php $name = $_POST['name']; echo "Hello, $name!"; ?>
This safely gets the 'email' input or shows a default message if empty.
PHP
<?php $email = $_POST['email'] ?? 'No email provided'; echo "Email: $email"; ?>
This checks if the form was submitted before using the data.
PHP
<?php if (isset($_POST['submit'])) { $message = $_POST['message']; echo "You wrote: $message"; } ?>
Sample Program
This page shows a form asking for a username. When you type your name and press Send, it greets you below the form. It uses $_POST to get the username safely.
PHP
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Simple Form</title> </head> <body> <form method="post" action=""> <label for="username">Username:</label> <input type="text" id="username" name="username" required> <br><br> <input type="submit" name="submit" value="Send"> </form> <?php if (isset($_POST['submit'])) { $user = htmlspecialchars($_POST['username']); echo "<p>Hello, $user! Welcome to the site.</p>"; } ?> </body> </html>
OutputSuccess
Important Notes
Always use htmlspecialchars() or similar to avoid security issues when showing user input.
$_POST only works if your form uses method="post".
If the form is not submitted, $_POST will be empty, so check with isset() before using.
Summary
$_POST collects data sent by a form using the POST method.
Use the input's name attribute to get its value from $_POST.
Always check if the form was submitted before using $_POST data.