$_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.
$_POST for form submissions in 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.
<?php $name = $_POST['name']; echo "Hello, $name!"; ?>
<?php $email = $_POST['email'] ?? 'No email provided'; echo "Email: $email"; ?>
<?php if (isset($_POST['submit'])) { $message = $_POST['message']; echo "You wrote: $message"; } ?>
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.
<!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>
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.
$_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.
Practice
$_POST variable in PHP do?Solution
Step 1: Understand the purpose of $_POST
$_POST is a superglobal array in PHP that collects data sent by a form using the POST method.Step 2: Differentiate from other methods
Unlike $_GET, which collects data sent via URL parameters, $_POST collects data sent in the request body.Final Answer:
It collects data sent by a form using the POST method. -> Option DQuick Check:
$_POST collects POST form data [OK]
- Confusing $_POST with $_GET
- Thinking $_POST sends data instead of receiving
- Assuming $_POST deletes data
username sent via POST in PHP?Solution
Step 1: Identify correct syntax for accessing $_POST data
$_POST is an associative array, so we use square brackets and quotes to access values by key.Step 2: Check each option
$_POST['username']; uses $_POST['username'], which is the correct syntax. Others use invalid syntax for arrays in PHP.Final Answer:
$_POST['username']; -> Option AQuick Check:
Use $_POST['name'] to get form data [OK]
- Using arrow (->) instead of brackets
- Missing quotes around key
- Using dot (.) notation for arrays
name=John via POST?<?php
if (isset($_POST['name'])) {
echo 'Hello, ' . $_POST['name'];
} else {
echo 'No name provided';
}
?>Solution
Step 1: Check if 'name' key exists in $_POST
The code uses isset($_POST['name']) which returns true if 'name' is sent via POST.Step 2: Output greeting with the name
Since 'name' is sent as 'John', the code echoes 'Hello, John'.Final Answer:
Hello, John -> Option CQuick Check:
isset($_POST['name']) true, output greeting [OK]
- Not checking isset and causing errors
- Assuming output is 'No name provided'
- Confusing POST and GET data
<?php
if ($_POST['email']) {
echo 'Email: ' . $_POST['email'];
} else {
echo 'No email provided';
}
?>Solution
Step 1: Analyze use of $_POST['email'] without isset
The code accesses $_POST['email'] directly without checking if it exists, which can cause an undefined index notice if form not submitted.Step 2: Explain proper check
Use isset($_POST['email']) to verify the key exists before accessing it.Final Answer:
No check if $_POST['email'] is set before use -> Option BQuick Check:
Always check isset($_POST['key']) before use [OK]
- Accessing $_POST keys without isset
- Confusing POST and GET methods
- Syntax errors in echo statement
username and password sent via POST. Which code snippet correctly checks if the form was submitted and then accesses both inputs?Solution
Step 1: Check form submission safely
<?php if ($_SERVER['REQUEST_METHOD'] === 'POST') { $user = $_POST['username'] ?? ''; $pass = $_POST['password'] ?? ''; echo "User: $user, Pass: $pass"; } else { echo 'Form not submitted'; } ?> uses $_SERVER['REQUEST_METHOD'] === 'POST' to confirm form submission, which is reliable.Step 2: Access inputs safely with null coalescing
It uses $_POST['username'] ?? '' to avoid undefined index errors if inputs are missing.Step 3: Compare other options
<?php if (isset($_POST['submit'])) { $user = $_POST['username']; $pass = $_POST['password']; echo "User: $user, Pass: $pass"; } ?> depends on a submit button named 'submit' which may not exist. <?php if ($_POST) { echo $_POST['username'] . $_POST['password']; } ?> lacks isset checks. <?php if ($_POST['username'] && $_POST['password']) { echo 'Logged in'; } ?> risks errors if keys missing.Final Answer:
Option A code snippet -> Option AQuick Check:
Use $_SERVER['REQUEST_METHOD'] and ?? for safe POST access [OK]
- Not verifying form submission method
- Assuming submit button always sent
- Accessing $_POST keys without fallback
Ready to prove it?
Pass the test and this pattern moves unknown
