我正在尝试设置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();
?>
答案 0 :(得分: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>";