1

在下面的代码中,我有 2 个下拉菜单。一个是“课程”下拉菜单,另一个是“模块”下拉菜单:

下面是代码:

   <?php

    // connect to the database
    include('connect.php');

    /* check connection */
    if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    die();
    }

    $sql = "SELECT CourseId, CourseName FROM Course ORDER BY CourseId"; 

    $sqlstmt=$mysqli->prepare($sql);

    $sqlstmt->execute(); 

    $sqlstmt->bind_result($dbCourseId, $dbCourseName);

    $courses = array(); // easier if you don't use generic names for data 

    $courseHTML = "";  
    $courseHTML .= '<select name="courses" id="coursesDrop" onchange="getModules();">'.PHP_EOL; 
    $courseHTML .= '<option value="">Please Select</option>'.PHP_EOL;  

    while($sqlstmt->fetch()) 
    { 
    $course = $dbCourseId;
    $coursename = $dbCourseName; 
    $courseHTML .= "<option value='".$course."'>" . $course . " - " . $coursename . "</option>".PHP_EOL;  
    } 

    $courseHTML .= '</select>'; 

    $moduleHTML = "";  
    $moduleHTML .= '<select name="modules" id="modulesDrop">'.PHP_EOL; 
    $moduleHTML .= '<option value="">Please Select</option>'.PHP_EOL;  
    $moduleHTML .= '</select>'; 


    include('noscript.php');

    ?>

    <script type="text/javascript">

    function getModules() { 
    var course = jQuery("#coursesDrop").val(); 
    jQuery('#modulesDrop').empty(); 
    jQuery('#modulesDrop').html('<option value="">Please Select</option>'); 
    jQuery.ajax({ 
    type: "post", 
    url:  "module.php", 
    data: { course:course }, 
    success: function(response){ 
    jQuery('#modulesDrop').append(response); 
    } 
    }); 


    }

    </script> 

<h1>EDIT AN ASSESSMENT'S DATE/START TIME</h1>    

<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<table>
<tr>
<th>Course: <?php echo $courseHTML; ?></th>
<th>Module: <?php echo $moduleHTML; ?></th>
</tr>
</table>
<p><input id="moduleSubmit" type="submit" value="Submit" name="moduleSubmit" /></p>
</form>

<?php

if (isset($_POST['moduleSubmit'])) {

$sessionquery = "
SELECT SessionId, SessionDate, SessionTime, ModuleId, TeacherId
FROM Session
WHERE
(ModuleId = ? AND TeacherId = ?)
ORDER BY SessionDate, SessionTime 
";

$sessionqrystmt=$mysqli->prepare($sessionquery);
// You only need to call bind_param once
$sessionqrystmt->bind_param("si",$moduleId,$userid);
// get result and assign variables (prefix with db)

$sessionqrystmt->execute(); 

$sessionqrystmt->bind_result($dbSessionId,$dbSessionDate,$dbSessionTime, $dbModuleId, $dbTeacherId);

$sessionqrystmt->store_result();

$sessionnum = $sessionqrystmt->num_rows();   

if($sessionnum ==0){
echo "<p>Sorry, You have No Sessions under this Module</p>";

module.php 在其中显示“模块”下拉菜单:

 <?php

         // connect to the database
     include('connect.php');

     /* check connection */
     if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    die();
    }


    $course = isset($_POST['course']) ? $_POST['course'] : ''; 

    $sql = "
    SELECT cm.CourseId, cm.ModuleId, 
    c.CourseName,
    m.ModuleName
    FROM Course c
    INNER JOIN Course_Module cm ON c.CourseId = cm.CourseId
    JOIN Module m ON cm.ModuleId = m.ModuleId
    WHERE
    (c.CourseId = ?)
    ORDER BY c.CourseId, m.ModuleId
    "; 

     $sqlstmt=$mysqli->prepare($sql);

     $sqlstmt->bind_param("s",$course);

     $sqlstmt->execute(); 

     $sqlstmt->bind_result($dbCourseId,$dbModuleId,$dbCourseName,$dbModuleName);


    $moduleHTML  = "";  

     while($sqlstmt->fetch()) { 
        $moduleHTML .= "<option value='$dbModuleId'>" . $dbModuleId . " - " . $dbModuleName . "</option>".PHP_EOL;   
    } 


    echo $moduleHTML; 

     $sqlstmt->execute(); 


    ?>

现在,如果您查看代码的底部,会发生什么情况是它在用户提交“课程”和“模块”下拉菜单后执行查询。如果它没有找到与该模块相关的会话,那么它会回显"Sorry, You have No Sessions under this Module"

但是我的问题是,如果用户没有从下拉菜单中选择“课程”和“模块”,或者用户从“课程”下拉菜单中选择了课程,我不希望显示此回声但尚未从“模块”下拉菜单中选择模块。

下面是我想要的每种情况的回声:

  1. 如果用户没有选择课程和模块:echo "Please Select a Course and Module";
  2. 如果用户选择了课程但没有选择模块:echo "Please Select a Module";
  3. 如果用户选择了课程和模块但没有出现会话:echo "Sorry, You have No Sessions under this Module";

目前上面的列表 3 有效,但我怎样才能让列表 1 和 2 只显示它们的回声而不显示其他回声?

更新:

验证下拉菜单的 Javascript 代码:

     function validation() {

                var isDataValid = true;

                var moduleTextO = document.getElementById("coursesDrop");
            var courseTextO = document.getElementById("modulesDrop");

            var errModuleMsgO = document.getElementById("moduleAlert");

  if (courseTextO.value == "" && moduleTextO.value == ""){
          errModuleMsgO.innerHTML = "Please Select a Course and Module";
          isDataValid = false;
  } else if (courseTextO.value == ""){
          errModuleMsgO.innerHTML = "Please Select a Course";
          isDataValid = false;      
  }else  if (moduleTextO.value == ""){
          errModuleMsgO.innerHTML = "Please Select a Module";
          isDataValid = false;    
    }else{
                errModuleMsgO.innerHTML = ""; 
        }

        return isDataValid;

        }

下面是HTML代码:

<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<table>
<tr>
<th>Course: <?php echo $courseHTML; ?></th>
<th>Module: <?php echo $moduleHTML; ?></th>
</tr>
</table>
<p><input id="moduleSubmit" type="submit" value="Submit" name="moduleSubmit" /></p>
<div id="moduleAlert"></div>
</form>
4

2 回答 2

1

您可以使用 javascript 在其中创建一个函数来执行此操作,您可以在其中检查天气每个字段是否已填充或不能像这样显示在那里

<script type="text/javascript">
function validate()
{
if(document.dropdownmenuname.course.value=="")
{
alert("Please fill in your form");
document.dropdownmenuname.course.focus() ;
 return false;
}
}
....
于 2012-10-27T21:14:04.023 回答
0

更改表单标签

<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">

<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post" onsubmit="return validation();">

在服务器端进行验证也是一个好主意,如果您想在服务器端检查值,只需在现有 PHP 中添加一些代码:

<?php

if (isset($_POST['moduleSubmit'])) {

if(empty($_POST['courses']) && empty($_POST['modules'])) {
   echo 'Please Select a Course and Module';
} elseif(empty($_POST['courses'])) {
   echo 'Please Select a Course';
} elseif(empty($_POST['modules'])) {
   echo 'Please Select a module';
} else {

$sessionquery = "
SELECT SessionId, SessionDate, SessionTime, ModuleId, TeacherId
FROM Session
WHERE
(ModuleId = ? AND TeacherId = ?)
ORDER BY SessionDate, SessionTime 
";

$sessionqrystmt=$mysqli->prepare($sessionquery);
// You only need to call bind_param once
$sessionqrystmt->bind_param("si",$moduleId,$userid);
// get result and assign variables (prefix with db)

$sessionqrystmt->execute(); 

$sessionqrystmt->bind_result($dbSessionId,$dbSessionDate,$dbSessionTime, $dbModuleId, $dbTeacherId);

$sessionqrystmt->store_result();

$sessionnum = $sessionqrystmt->num_rows();   

if($sessionnum ==0){
echo "<p>Sorry, You have No Sessions under this Module</p>";

}
于 2012-10-27T22:28:06.483 回答