I am trying to validate a html form using php. But I’m having a hard time since our professor provided us a pre coded activity.
Code goes like this,
if(isset($_POST['save'])){
$firstname = htmlentities($_POST['fn']);
$lastname = htmlentities($_POST['ln']);
$email = htmlentities($_POST['email']);
$status = 'Inactive';
if(isset($_POST['status'])){
$status = $_POST['status'];
}
$faculty = array(
"firstname" => $firstname,
"lastname" => $lastname,
"email" => $email,
"academic_rank" => $_POST['rank'],
"department" => $_POST['department'],
"admission_role" => $_POST['role'],
"status" => $status
);
array_push($_SESSION['faculty'], $faculty);
header('location: faculty.php');
}
?>
<form class="add-faculty" action="addfaculty.php" method="post">
<label for="fn">First Name</label>
<input type="text" id="fn" name="fn" required placeholder="Enter first name">
<label for="ln">Last Name</label>
<input type="text" id="ln" name="ln" required placeholder="Enter last name">
<label for="email">Email</label>
<input type="email" id="email" name="email" required placeholder="Enter email">
<label for="rank">Academic Rank</label>
<select name="rank" id="rank">
<option value="None">--Select--</option>
<option value="Instructor">Instructor</option>
<option value="Asst. Professor">Asst. Professor</option>
<option value="Asso. Professor">Asso. Professor</option>
<option value="Professor">Professor</option>
</select>
<label for="department">Department</label>
<select name="department" id="department">
<option value="None">--Select--</option>
<option value="Computer Science">Computer Science</option>
<option value="Information Technology">Information Technology</option>
</select>
<div>
<label for="role">Admission Role</label><br>
<label class="container" for="admin">Admission Officer
<input type="radio" name="role" id="admin" value="Admission Officer">
<span class="checkmark"></span>
</label>
<label class="container" for="interviewer">Interviewer
<input type="radio" name="role" id="interviewer" value="Interviewer" checked>
<span class="checkmark"></span>
</label>
</div>
<label for="status">Is Status of Employee Active?</label><br>
<label class="container" for="status">Yes
<input type="checkbox" name="status" id="status" value="Active Employee" checked>
<span class="checkbox"></span>
</label>
<div>
<input type="submit" class="button" value="Save Faculty" name="save" id="save">
</div>
</form>
So, how can I validate or sanitize the user input such as name, lastname, etc. Please help. Thank you.