Complete the code to get the value of the input named 'username' from the form submission.
<?php
$username = $_POST[[1]];
?>In PHP, to get a value from a form submitted via POST, you use $_POST with the input's name as a string key. So, $_POST["username"] gets the value of the input named 'username'.
Complete the code to check if the form was submitted by checking if the 'submit' button was pressed.
<?php if (isset($_POST[[1]])) { echo "Form submitted!"; } ?>
To check if the form was submitted, we check if the submit button's name exists in $_POST. Usually, the submit button is named 'submit', so we use isset($_POST["submit"]).
Fix the error in the code to safely get the 'email' input value and avoid undefined index errors.
<?php $email = $_POST[[1]] ?? ''; ?>
To avoid undefined index errors, use the null coalescing operator (??) with the correct string key. The input name is 'email', so use $_POST["email"] ?? ''.
Fill both blanks to create an associative array with input names as keys and their values from the form submission.
<?php
$data = [
'name' => $_POST[[1]],
'age' => $_POST[[2]]
];
?>To get values from $_POST, use the input names as string keys. Here, 'name' and 'age' are keys, so use $_POST["name"] and $_POST['age'].
Fill all three blanks to loop through $_POST and print each key and value.
<?php foreach ($_POST as [1] => [2]) { echo [3] . ': ' . [2] . "<br>"; } ?>
In a foreach loop over $_POST, use $key for keys and $value for values. To print the key, use $key.