0

当用户没有从下拉菜单中选择选项时,它不会显示错误消息“请选择课程”。任何人都可以看到它为什么不显示消息并查看我是否正确地进行了 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";
}
?>
4

2 回答 2

2

$errormsg在输出 HTML 后正在填充。因此,$errormsg在输出时始终为空白。

于 2012-11-30T21:28:22.120 回答
1

在页面加载的那一刻,$errormsg 尚未设置。尝试在 html 之前获取验证

于 2012-11-30T21:30:12.120 回答