I have a code below where it displays students details in a drop down menu, and then it does a php validation to see if a student is selected from the drop down menu or not:
$sql = "SELECT StudentUsername, StudentForename, StudentSurname FROM Student ORDER BY StudentUsername";
$sqlstmt = $mysqli->prepare($sql);
$sqlstmt->execute();
$sqlstmt->bind_result($dbStudentUsername, $dbStudentForename, $dbStudentSurname);
$students = array(); // easier if you don't use generic names for data
$studentHTML = "";
$studentHTML .= '<select name="students" id="studentsDrop">'.PHP_EOL;
$studentHTML .= '<option value="">Please Select</option>'.PHP_EOL;
$outputstudent = "";
while($sqlstmt->fetch())
{
$student = $dbStudentUsername;
$firstname = $dbStudentForename;
$surname = $dbStudentSurname;
$studentHTML .= "<option value='" . $student . "'>" . $student . " - " . $firstname . " " . $surname . "</option>" . PHP_EOL;
}
$studentHTML .= '</select>';
$errormsg = (isset($errormsg)) ? $errormsg : '';
if(isset($_POST['resetpass'])) {
//get the form data
$studentdrop = (isset($_POST['students'])) ? $_POST['students'] : '';
if($studentdrop = "") {
$errormsg = "Student is Selected";
}
else{
$errormsg = "You must Select a Student";
}
}
The problem I have though is that even though I have selected a student from the drop down menu, it still displays a message stating that "You Must select a Student".
What is suppose to happen is that if the user has not selected a student or in other words when they have submitted the form and the drop down menu is still on the "Please Select" option, then it displays the message stating that user must select a student, else if the user does select a student from the drop down menu then display the message that a student has been selected.