当用户没有从下拉菜单中选择选项时,它不会显示错误消息“请选择课程”。任何人都可以看到它为什么不显示消息并查看我是否正确地进行了 php 验证以便能够检查是否选择了下拉选项?
下面是代码:
$sql = "SELECT CourseId, CourseNo, CourseName FROM Course ORDER BY CourseNo";
$sqlstmt=$mysqli->prepare($sql);
$sqlstmt->execute();
$sqlstmt->bind_result($dbCourseId, $dbCourseNo, $dbCourseName);
$courses = array(); // easier if you don't use generic names for data
$courseHTML = "";
$courseHTML .= '<select name="courses" id="coursesDrop">'.PHP_EOL;
$courseHTML .= '<option value="">Please Select</option>'.PHP_EOL;
while($sqlstmt->fetch())
{
$courseno = $dbCourseNo;
$course = $dbCourseId;
$coursename = $dbCourseName;
$courseHTML .= "<option value='".$course."'>" . $courseno . " - " . $coursename . "</option>".PHP_EOL;
}
$courseHTML .= '</select>';
$submittedCourseId = (isset($_POST['courses'])) ? $_POST['courses'] : '';
$errormsg = (isset($errormsg)) ? $errormsg : '';
?>
<h1>CREATING A NEW ASSESSMENT</h1>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<table>
<tr>
<th>Course: <?php echo $courseHTML; ?><input id="courseSubmit" type="submit" value="Submit" name="submit" /></th>
</tr>
<tr>
<td id="errormsg"><?php echo $errormsg; ?></td>
</tr>
</table>
</form>
<?php
if (isset($_POST['submit'])) {
$submittedCourseId = $_POST['courses'];
if($submittedCourseId){
?>
//need to do functions here
<?php
}
else{
$errormsg = "Please Select a Course";
}
?>