Complete the code to check if the form was submitted using POST method.
<?php if ($_SERVER['REQUEST_METHOD'] == '[1]') { echo "Form submitted!"; } ?>
The form submission method is checked by comparing $_SERVER['REQUEST_METHOD'] to POST.
Complete the code to safely get the submitted username from the form.
<?php if ($_SERVER['REQUEST_METHOD'] == 'POST') { $username = $_POST['[1]'] ?? ''; echo "Hello, " . htmlspecialchars($username); } ?>
The form field name for the username is usually 'username'. Using $_POST['username'] retrieves the submitted value.
Fix the error in the code to check if the form was submitted and the email field is not empty.
<?php if ($_SERVER['REQUEST_METHOD'] == 'POST' && !empty($_POST['[1]'])) { echo "Email received."; } ?>
The form field for email is usually named 'email'. Checking !empty($_POST['email']) ensures the field is filled.
Fill both blanks to create an associative array of submitted form data with keys 'name' and 'age'.
<?php if ($_SERVER['REQUEST_METHOD'] == 'POST') { $data = [ '[1]' => $_POST['name'], '[2]' => (int)$_POST['age'] ]; print_r($data); } ?>
The array keys should match the data they hold: 'name' for the name and 'age' for the age.
Fill all three blanks to validate and sanitize the submitted email and password fields.
<?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."; } } ?>
The email field is named 'email', the password field 'password', and a common minimum password length is 8 characters.