0

我正在尝试设置 2 个下拉菜单,一个用于“课程”,另一个用于选择课程的相关“模块”。

现在“课程”下拉菜单工作正常。问题是“模块”下拉菜单。如果我从“课程”下拉菜单中选择一门课程,那么它应该在“模块”下拉菜单中显示与所选课程相关的模块。但相反,它只显示“模块”下拉菜单的“请选择”选项,没有别的。

如何让相关模块出现在“模块”下拉菜单中?

下面是显示“课程”下拉菜单和“模块”下拉菜单设置的代码,它包含一个 jquery,它链接到“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();
}

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

$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>'; 
$courseHTML .= '</form>'; 

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


?>

<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> 

下面是 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.CourseName, 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 . "</option>".PHP_EOL;   
} 


echo $moduleHTML; 

 $sqlstmt->execute(); 


?>
4

1 回答 1

0

将模块放入 div 并绘制 SELECT 标签

最好将这一切都作为一个 jquery“加载”

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

$moduleHTML .= "<select >";
 while($sqlstmt->fetch()) { 
    $moduleHTML .= "<option value='$dbModuleId'>" . $dbModuleId . "</option>".PHP_EOL;   
} 
$moduleHTML .= "</select>";
于 2012-10-26T01:22:02.527 回答