Adapt the solution to Exercise 1 (Buggy HTML Form) to create a PHP script with form validation.
uwnetid value must be exactly 8 (eight) acceptable characters: alphabetic, numeric, an underscore ('_'), or a dash ('-').year value must be one of the four accepted values.studentid value must be exactly 7 (seven) numeric characters.essay value should have between 450 and 550 words. Assume a word is a sequence of one or more alphabetic or numeric characters. To account for punctuation and spacing, assume that words are separated by anything that isn’t alphabetic or numeric.
<?php
$valid = true;
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$patterns = array(
'uwnetid' => '/^[a-z_\-]{8}$/i',
'year' => '/^frosh|soph|junior|senior$/',
'studentid' => '/^\d{7}$/',
'essay' => '/^[a-z0-9]+([^a-z0-9]+[a-z0-9]+){449,549}$/'
);
foreach ($patterns as $param => $pattern) {
if (!isset($_POST[$param]) || !preg_match($pattern, $_POST[$param])) {
$valid = false;
break;
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Essay Submission</title>
</head>
<body>
<?php if ($_SERVER['REQUEST_METHOD'] == 'GET') { ?>
<form action="" method="post">
<!-- (form controls here) -->
</form>
<?php } else if ($_SERVER['REQUEST_METHOD'] == 'POST') { ?>
<?php if (!$valid) { ?>
<p>ERROR: You submitted an invalid value.</p>
<?php } else { ?>
<p>Thank you for your submission, <?= $_POST['uwnetid'] ?>!</p>
<?php } ?>
<?php } ?>
</body>
</html>