0
0
PHPprogramming~10 mins

$_POST for form submissions 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 get the value of the input named 'username' from the form submission.

PHP
<?php
$username = $_POST[[1]];
?>
Drag options to blanks, or click blank then click option'
A"user"
Busername
C"username"
D'user'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting quotes around the input name.
Using the variable name without quotes.
Using the wrong input name.
2fill in blank
medium

Complete the code to check if the form was submitted by checking if the 'submit' button was pressed.

PHP
<?php
if (isset($_POST[[1]])) {
    echo "Form submitted!";
}
?>
Drag options to blanks, or click blank then click option'
A"submit"
B"button"
C'submit_button'
D"send"
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for the wrong key in $_POST.
Not using isset() to check existence.
Using single quotes inconsistently.
3fill in blank
hard

Fix the error in the code to safely get the 'email' input value and avoid undefined index errors.

PHP
<?php
$email = $_POST[[1]] ?? '';
?>
Drag options to blanks, or click blank then click option'
Auser_email
B'mail'
Cemail
D"email"
Attempts:
3 left
💡 Hint
Common Mistakes
Using the input name without quotes.
Using the wrong input name.
Not using the null coalescing operator.
4fill in blank
hard

Fill both blanks to create an associative array with input names as keys and their values from the form submission.

PHP
<?php
$data = [
    'name' => $_POST[[1]],
    'age' => $_POST[[2]]
];
?>
Drag options to blanks, or click blank then click option'
A"name"
B"age"
C'age'
D'username'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing single and double quotes inconsistently.
Using wrong input names.
Forgetting quotes around keys.
5fill in blank
hard

Fill all three blanks to loop through $_POST and print each key and value.

PHP
<?php
foreach ($_POST as [1] => [2]) {
    echo [3] . ': ' . [2] . "<br>";
}
?>
Drag options to blanks, or click blank then click option'
A$key
B$value
D$item
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same variable for key and value.
Using undefined variables.
Forgetting the arrow => in foreach.