0
0
PHPprogramming~10 mins

Form handling execution flow in PHP - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if the form was submitted using POST method.

PHP
<?php
if ($_SERVER['REQUEST_METHOD'] == '[1]') {
    echo "Form submitted!";
}
?>
Drag options to blanks, or click blank then click option'
AGET
BDELETE
CPUT
DPOST
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'GET' instead of 'POST' to check the form submission.
Not using quotes around the method name.
2fill in blank
medium

Complete the code to safely get the submitted username from the form.

PHP
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $username = $_POST['[1]'] ?? '';
    echo "Hello, " . htmlspecialchars($username);
}
?>
Drag options to blanks, or click blank then click option'
Ausername
Bname
Cuser
Dlogin
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong key like 'user' or 'name' that does not match the form field.
Not using the null coalescing operator to avoid undefined index errors.
3fill in blank
hard

Fix the error in the code to check if the form was submitted and the email field is not empty.

PHP
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST' && !empty($_POST['[1]'])) {
    echo "Email received.";
}
?>
Drag options to blanks, or click blank then click option'
Ae-mail
Bmail
Cemail
Duser_email
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect field names like 'mail' or 'e-mail'.
Not checking if the field is empty before processing.
4fill in blank
hard

Fill both blanks to create an associative array of submitted form data with keys 'name' and 'age'.

PHP
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $data = [
        '[1]' => $_POST['name'],
        '[2]' => (int)$_POST['age']
    ];
    print_r($data);
}
?>
Drag options to blanks, or click blank then click option'
Aname
Busername
Cage
Dyears
Attempts:
3 left
💡 Hint
Common Mistakes
Using keys that do not match the form fields.
Mixing up 'age' and 'years' as keys.
5fill in blank
hard

Fill all three blanks to validate and sanitize the submitted email and password fields.

PHP
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $email = filter_var($_POST['[1]'], FILTER_VALIDATE_EMAIL);
    $password = trim($_POST['[2]']);
    if ($email && strlen($password) >= [3]) {
        echo "Valid input.";
    } else {
        echo "Invalid input.";
    }
}
?>
Drag options to blanks, or click blank then click option'
Aemail
Bpassword
C8
D6
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong field names like 'user_email' or 'pass'.
Setting password length too low or forgetting to trim whitespace.