0
0
PHPprogramming~20 mins

Form handling execution flow in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Form Handling 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 form handling code?

Consider this PHP script that processes a form submission. What will it output when the form is submitted with name=John?

PHP
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = $_POST['name'] ?? 'Guest';
    echo "Hello, $name!";
} else {
    echo "Please submit the form.";
}
?>
AUndefined index: name
BHello, John!
CHello, Guest!
DPlease submit the form.
Attempts:
2 left
💡 Hint

Check the request method and how the $_POST array is used.

Predict Output
intermediate
2:00remaining
What happens if the form is submitted with GET method?

Given this PHP code snippet, what will be the output if the form is submitted using the GET method with name=Alice?

PHP
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    echo "Hello, " . htmlspecialchars($_POST['name']) . "!";
} else {
    echo "Form not submitted via POST.";
}
?>
AForm not submitted via POST.
BHello, Alice!
CUndefined index: name
DHello, !
Attempts:
2 left
💡 Hint

Look at the condition checking the request method.

🔧 Debug
advanced
2:30remaining
Why does this PHP form handler show an error when no input is given?

Examine this PHP code snippet. When the form is submitted without the 'email' field, it shows an error. What causes this error?

PHP
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $email = $_POST['email'];
    echo "Email: $email";
}
?>
AThe variable $email is not declared before use.
BThe form method is incorrect, it should be GET.
CThe echo statement syntax is invalid.
DAccessing <code>$_POST['email']</code> without checking if it exists causes an undefined index error.
Attempts:
2 left
💡 Hint

Think about what happens if a key is missing in the $_POST array.

📝 Syntax
advanced
2:00remaining
Identify the syntax error in this PHP form processing code

What is the syntax error in this PHP snippet that handles form data?

PHP
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $username = $_POST['username'];
    echo "User: $username";
} else {
    echo "No data received."
}
?>
AUsing single quotes instead of double quotes in echo.
BIncorrect comparison operator in the if condition.
CMissing semicolon after the echo statement in the else block.
DVariable $username is not initialized.
Attempts:
2 left
💡 Hint

Check the end of each statement carefully.

🚀 Application
expert
3:00remaining
How many items are in the $_POST array after submitting this form?

Given this HTML form and PHP script, how many key-value pairs will $_POST contain after submission?

HTML form:

<form method="POST" action="submit.php">
  <input type="text" name="first_name" value="Anna" />
  <input type="text" name="last_name" value="Smith" />
  <input type="checkbox" name="subscribe" value="yes" checked />
  <input type="checkbox" name="subscribe" value="no" />
  <input type="submit" value="Send" />
</form>

PHP snippet (submit.php):

<?php
print_r($_POST);
?>
A3
B2
C1
D4
Attempts:
2 left
💡 Hint

Remember how checkboxes with the same name behave in form submission.