Bird
Raised Fist0
PHPprogramming~20 mins

$_POST for form submissions in PHP - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Post Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this PHP code using $_POST?
Consider a form submission where the form sends a POST request with a field named 'username' set to 'Alice'. What will this PHP code output?
PHP
<?php
if (isset($_POST['username'])) {
    echo "Hello, " . $_POST['username'] . "!";
} else {
    echo "No username provided.";
}
?>
AUndefined index: username
BNo username provided.
CHello, !
DHello, Alice!
Attempts:
2 left
💡 Hint
Remember that $_POST contains data sent by the form using the POST method.
🧠 Conceptual
intermediate
1:30remaining
Which statement about $_POST is true?
Choose the correct statement about the PHP $_POST superglobal array.
A$_POST contains data sent via the HTTP POST method.
B$_POST contains data sent via the HTTP GET method.
C$_POST is used to store session variables.
D$_POST is an object that holds form data.
Attempts:
2 left
💡 Hint
Think about how form data is sent using different HTTP methods.
🔧 Debug
advanced
2:00remaining
Why does this PHP code produce an error when accessing $_POST?
What error will this code produce and why?
PHP
<?php
echo $_POST['email'];
?>
ASyntax error because $_POST is not defined.
BFatal error: Cannot access $_POST directly.
CUndefined index: email if the form did not send 'email'.
DNo output because $_POST is empty but no error.
Attempts:
2 left
💡 Hint
What happens if you try to access an array key that does not exist?
📝 Syntax
advanced
2:30remaining
Which option correctly checks if a POST field 'age' is set and not empty?
Select the correct PHP code snippet that safely checks if the 'age' field was submitted and is not empty.
Aif (isset($_POST['age']) || $_POST['age'] != '') { echo 'Age is set'; }
Bif (isset($_POST['age']) && $_POST['age'] !== '') { echo 'Age is set'; }
Cif ($_POST['age']) { echo 'Age is set'; }
Dif (!empty($_POST['age'])) { echo 'Age is set'; } else { echo 'Age not set'; }
Attempts:
2 left
💡 Hint
Use isset() to check if the key exists and compare to empty string to ensure it's not empty.
🚀 Application
expert
3:00remaining
How many items are in $_POST after submitting this form?
Given this HTML form submitted via POST:

How many key-value pairs will $_POST contain after submission?
A1
B3
C2
D0
Attempts:
2 left
💡 Hint
Checkboxes only send their value if checked. Multiple inputs with the same name overwrite or combine values.

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

  1. 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.
  2. Step 2: Differentiate from other methods

    Unlike $_GET, which collects data sent via URL parameters, $_POST collects data sent in the request body.
  3. Final Answer:

    It collects data sent by a form using the POST method. -> Option D
  4. 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

  1. 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.
  2. Step 2: Check each option

    $_POST['username']; uses $_POST['username'], which is the correct syntax. Others use invalid syntax for arrays in PHP.
  3. Final Answer:

    $_POST['username']; -> Option A
  4. 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

  1. Step 1: Check if 'name' key exists in $_POST

    The code uses isset($_POST['name']) which returns true if 'name' is sent via POST.
  2. Step 2: Output greeting with the name

    Since 'name' is sent as 'John', the code echoes 'Hello, John'.
  3. Final Answer:

    Hello, John -> Option C
  4. 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:
<?php
if ($_POST['email']) {
    echo 'Email: ' . $_POST['email'];
} else {
    echo 'No email provided';
}
?>
medium
A. Missing quotes around 'email' key
B. No check if $_POST['email'] is set before use
C. Using GET method instead of POST
D. Incorrect echo syntax

Solution

  1. 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.
  2. Step 2: Explain proper check

    Use isset($_POST['email']) to verify the key exists before accessing it.
  3. Final Answer:

    No check if $_POST['email'] is set before use -> Option B
  4. 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?
hard
A. <?php if ($_SERVER['REQUEST_METHOD'] === 'POST') { $user = $_POST['username'] ?? ''; $pass = $_POST['password'] ?? ''; echo "User: $user, Pass: $pass"; } else { echo 'Form not submitted'; } ?>
B. <?php if (isset($_POST['submit'])) { $user = $_POST['username']; $pass = $_POST['password']; echo "User: $user, Pass: $pass"; } ?>
C. <?php if ($_POST) { echo $_POST['username'] . $_POST['password']; } ?>
D. <?php if ($_POST['username'] && $_POST['password']) { echo 'Logged in'; } ?>

Solution

  1. 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.
  2. Step 2: Access inputs safely with null coalescing

    It uses $_POST['username'] ?? '' to avoid undefined index errors if inputs are missing.
  3. 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.
  4. Final Answer:

    Option A code snippet -> Option A
  5. Quick Check:

    Use $_SERVER['REQUEST_METHOD'] and ?? for safe POST access [OK]
Hint: Check $_SERVER['REQUEST_METHOD'] and use ?? to avoid errors [OK]
Common Mistakes:
  • Not verifying form submission method
  • Assuming submit button always sent
  • Accessing $_POST keys without fallback
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard