📖 Scenario: You are creating a simple web form where users can enter their name and email. You want to handle the form submission in PHP, check if the form was submitted, and then display a thank you message with the entered data.
🎯 Goal: Build a PHP script that shows a form, checks if the form was submitted, processes the input data, and then displays a thank you message with the user's name and email.
📋 What You'll Learn
Create a form with name and email input fields
Use a PHP variable form_submitted to check if the form was submitted
Retrieve the name and email from the $_POST array
Display a thank you message with the submitted name and email
💡 Why This Matters
🌍 Real World
Forms are everywhere on the web for collecting user data like signups, contact info, and feedback.
💼 Career
Understanding form handling is essential for web developers to create interactive and user-friendly 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 type="submit".
PHP
Hint
Use the <form> tag with method="post". Add two inputs with name="name" and name="email". Add a submit button.
2
Add a PHP variable to check form submission
Add a PHP variable called form_submitted that is true if $_SERVER['REQUEST_METHOD'] equals 'POST', otherwise false.
PHP
Hint
Use $_SERVER['REQUEST_METHOD'] to check if the form was submitted with POST.
3
Retrieve form data when submitted
If form_submitted is true, create two variables name and email that get their values from $_POST['name'] and $_POST['email'] respectively.
PHP
Hint
Use if ($form_submitted) and then assign $name = $_POST['name']; and $email = $_POST['email'];.
4
Display thank you message after submission
If form_submitted is true, print a thank you message using echo that says: Thank you, [name]! We will contact you at [email]. replacing [name] and [email] with the variables $name and $email.
PHP
Hint
Use echo to print the message with variables $name and $email inside double quotes.
Practice
(1/5)
1. What is the main purpose of checking $_SERVER['REQUEST_METHOD'] == 'POST' in PHP form handling?
easy
A. To display the form to the user
B. To ensure the form data is processed only when the form is submitted
C. To reset the form fields
D. To connect to the database
Solution
Step 1: Understand the role of $_SERVER['REQUEST_METHOD']
This variable tells us how the page was requested, for example, via GET or POST.
Step 2: Check why we compare it to 'POST'
When the form is submitted, it usually sends data via POST. Checking this ensures we only process data after submission.
Final Answer:
To ensure the form data is processed only when the form is submitted -> Option B
Quick Check:
Request method check = process form only after submit [OK]
Hint: Process form only if request method is POST [OK]
Common Mistakes:
Processing form data without checking request method
Confusing GET and POST methods
Trying to process form before submission
2. Which of the following is the correct way to start processing form data only after submission in PHP?
easy
A. if ($_SERVER['REQUEST_METHOD'] == 'POST') { /* process form */ }
B. if ($_SERVER['REQUEST_METHOD'] = 'POST') { /* process form */ }
C. if ($_POST) { /* process form */ }
D. if ($_GET['submit']) { /* process form */ }
Solution
Step 1: Identify correct comparison operator
if ($_SERVER['REQUEST_METHOD'] = 'POST') { /* process form */ } uses a single equals sign (=) which assigns, not compares. This is incorrect.
Step 2: Check proper request method check
if ($_SERVER['REQUEST_METHOD'] == 'POST') { /* process form */ } correctly uses double equals (==) to compare $_SERVER['REQUEST_METHOD'] to 'POST'.
Final Answer:
if ($_SERVER['REQUEST_METHOD'] == 'POST') { /* process form */ } -> Option A
Quick Check:
Use '==' to compare request method [OK]
Hint: Use '==' to compare request method, not '=' [OK]
Common Mistakes:
Using '=' instead of '==' for comparison
Checking $_POST without verifying request method
Using $_GET when form uses POST
3. What will be the output of this PHP code when the form is submitted with name=John?
A. Echoing variable outside the if block without initialization
B. Using $_POST without checking if 'email' exists
C. Missing semicolon after echo statement
D. Incorrect request method check
Solution
Step 1: Analyze variable scope and initialization
The variable $email is assigned only inside the if block, so if the request method is not POST, it remains undefined.
Step 2: Check usage of variable outside if block
Echoing $email outside the if block without initialization causes a notice or error if the form is not submitted.
Final Answer:
Echoing variable outside the if block without initialization -> Option A
Quick Check:
Use variable only after initialization [OK]
Hint: Initialize variables before use outside condition [OK]
Common Mistakes:
Assuming variables inside if exist outside
Not initializing variables before echo
Ignoring request method check
5. You want to keep the user's input visible in the form field after submission to improve user experience. Which PHP code snippet correctly achieves this for a text input named username?
hard
A. <input type='text' name='username' value='<?php echo $_POST['username']; ?>'>
B. <input type='text' name='username' value='<?php echo $_GET['username']; ?>'>
C. <input type='text' name='username' value='<?php echo htmlspecialchars($_POST['username'] ?? ''); ?>'>
D. <input type='text' name='username' value='<?php echo $_POST['username'] ?? 'Guest'; ?>'>
Solution
Step 1: Ensure safe output in HTML attribute
Using htmlspecialchars() prevents HTML injection and ensures special characters display correctly.
Step 2: Use null coalescing operator to avoid undefined index
$_POST['username'] ?? '' ensures the value is empty string if not set, avoiding errors.