php验证消息没有出现

时间:2012-11-30 21:25:57

标签: php validation

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

2 个答案:

答案 0 :(得分:2)

在输出HTML之后,

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

答案 1 :(得分:1)

在页面加载时,尚未设置$ errormsg。尝试在html之前获得验证