Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help with this Your assignment is: Update the form to be a PHP self posti

ID: 3733175 • Letter: I

Question

I need help with this Your assignment is: Update the form to be a PHP self posting form. Implement PHP validations for each field. The validation requirements for each field are listed on the page. You do NOT need Javascript validations for this project. Validate the input fields on the server using PHP. Use regular expressions and patterns for this whenever possible. If there are any invalid fields display the appropriate error messages, apply a CSS style to the error fields. Put all form data back into the appropriate fields. Display to the user. Protect your form with one of the methods discussed in the class. When the form inputs are valid display a confirmation message to the page instead of the form. attached the student info varioes jpg files

Explanation / Answer

<!DOCTYPE HTML>  
<html>
<head>
<style>
.error {color: #CC0000;}
</style>
</head>
<body>  

<?php
// defining variables and set them to empty values
$nameError= $emailError = $genderError ="";
$name = $email = $gender = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
  if (empty($_POST["name"])) {
   $nameError = "Name is required";
  } else {
   $name = test_input($_POST["name"]);
   // check if name only contains letters and whitespace
   if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
   $nameError = "Only letters and white space are allowed";
   }
  }
  
  if (empty($_POST["email"])) {
   $emailErr = "Email is required";
  } else {
   $email = test_input($_POST["email"]);
   // email validation
   if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
   $emailError= " Email is in invalid format";
   }
  }
  
if (empty($_POST["gender"])) {
   $genderErr = "Gender is required";
  } else {
   $gender = test_input($_POST["gender"]);
  }
}

function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}
?>

<h2>PHP Form </h2>
<p><span class="error">* required field.</span></p>
<form method="post" action="<?php echohtmlspecialchars($_SERVER["PHP_SELF"]);?>">  
  Name: <input type="text" name="name">
  <span class="error">* <?php echo $nameError;?></span>
  <br><br>
  E-mail: <input type="text" name="email">
  <span class="error">* <?php echo $emailError;?></span>
  <br><br>

  Gender:
  <input type="radio" name="gender" value="female">Female
  <input type="radio" name="gender" value="male">Male
  <span class="error">* <?php echo $genderError;?></span>
  <br><br>
  <input type="submit" name="submit" value="Submit">  
</form>

<?php
echo "<h2>Input</h2>";
echo $name;
echo "<br>";
echo $email;
echo "<br>";

echo $gender;
?>

</body>
</html>