当用户未从下拉菜单中选择选项时,它不会显示错误消息“请选择课程”。任何人都可以看到为什么它没有显示消息,看看我是否正在进行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";
}
?>
答案 0 :(得分:2)
$errormsg
正在填充。因此,$errormsg
在输出时始终为空白。
答案 1 :(得分:1)
在页面加载时,尚未设置$ errormsg。尝试在html之前获得验证