📖 Scenario: You want to create a simple web form where users can enter their name and email. When they submit the form, the server will receive the data and display a welcome message with their name and email.
🎯 Goal: Build a PHP script that uses the $_POST superglobal to get form data and display it back to the user.
📋 What You'll Learn
Create an HTML form with method="post" and inputs for name and email.
Create PHP variables to store the submitted name and email from $_POST.
Use PHP to check if the form was submitted before accessing $_POST data.
Display a welcome message using the submitted name and email.
💡 Why This Matters
🌍 Real World
Forms are everywhere on the web for collecting user information like sign-ups, contact info, and feedback. Using $_POST is the standard way in PHP to handle form submissions securely.
💼 Career
Web developers often need to create forms and process user input. Understanding $_POST is essential for backend PHP programming and building interactive websites.
Progress0 / 4 steps
1
Create the HTML form
Create an HTML form with method="post" and two input fields: name="name" and name="email". Also add a submit button with the text Submit.
PHP
Hint
Use the <form> tag with method="post". Add two inputs with name attributes exactly as name and email.
2
Create PHP variables to hold submitted data
Create two PHP variables called $name and $email. Set them to empty strings initially.
PHP
Hint
Before the form, create $name and $email variables and set them to empty strings.
3
Assign submitted form data to PHP variables
Use an if statement to check if $_POST['name'] and $_POST['email'] are set. If yes, assign $_POST['name'] to $name and $_POST['email'] to $email.
PHP
Hint
Use isset() to check if the form data exists before using it.
4
Display the welcome message with submitted data
Add a print statement to display: "Welcome, $name! Your email is $email." below the form.
PHP
Hint
Use print() with double quotes and variables inside to show the message.
Practice
(1/5)
1. What does the $_POST variable in PHP do?
easy
A. It deletes form data after submission.
B. It stores data sent by a form using the GET method.
C. It sends data from the server to the client.
D. It collects data sent by a form using the POST method.
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 D
Quick Check:
$_POST collects POST form data [OK]
Hint: Remember: $_POST = form data sent via POST method [OK]
Common Mistakes:
Confusing $_POST with $_GET
Thinking $_POST sends data instead of receiving
Assuming $_POST deletes data
2. Which of the following is the correct way to access a form input named username sent via POST in PHP?
easy
A. $_POST['username'];
B. $_POST->username;
C. post['username'];
D. $_POST.username;
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 A
Quick Check:
Use $_POST['name'] to get form data [OK]
Hint: Use square brackets and quotes to access $_POST keys [OK]
Common Mistakes:
Using arrow (->) instead of brackets
Missing quotes around key
Using dot (.) notation for arrays
3. What will be the output of this PHP code if the form sends name=John via POST?
<?php
if (isset($_POST['name'])) {
echo 'Hello, ' . $_POST['name'];
} else {
echo 'No name provided';
}
?>
medium
A. Hello,
B. No name provided
C. Hello, John
D. Undefined index error
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 C
Quick Check:
isset($_POST['name']) true, output greeting [OK]
Hint: Check isset before using $_POST to avoid errors [OK]
Common Mistakes:
Not checking isset and causing errors
Assuming output is 'No name provided'
Confusing POST and GET data
4. Identify the error in this PHP code snippet handling a POST form:
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 B
Quick Check:
Always check isset($_POST['key']) before use [OK]
Hint: Always use isset() before accessing $_POST keys [OK]
Common Mistakes:
Accessing $_POST keys without isset
Confusing POST and GET methods
Syntax errors in echo statement
5. You want to create a PHP script that safely processes a form with inputs username and password sent via POST. Which code snippet correctly checks if the form was submitted and then accesses both inputs?