我认为我没有正确编写下面的代码,但我希望能够从另一个页面上的模块下拉菜单中检索值。下面是包含空白下拉菜单并输出所选模块的编号和名称的代码:
$sql = "SELECT CourseId, CourseNo, CourseName FROM Course";
$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" onchange="getModules();">'.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>';
$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>
<?php
if (isset($_POST['moduleSubmit'])) {
$outputmodule = "";
$moduleInfo = explode("_", $_POST['modules']);
$moduleId = $moduleInfo[0];
$moduleNo = $moduleInfo[1];
$moduleName = $moduleInfo[2];
$outputmodule = sprintf("<p><strong>Module:</strong> %s - %s</p>", $moduleNo, $moduleName);
....
?>
下面是 module.php 页面,它根据所选课程在下拉菜单中显示模块编号和名称:
<?php
$course = isset($_POST['course']) ? $_POST['course'] : '';
$sql = "
SELECT cm.CourseId, cm.ModuleId, c.CourseNo, m.ModuleNo,
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,$dbCourseNo,$dbModuleNo,$dbCourseName,$dbModuleName);
$moduleHTML = "";
while($sqlstmt->fetch()) {
$moduleHTML .= sprintf('<option value="%1$s">%2$s - %3$s</option>'.PHP_EOL, $dbModuleId, $dbModuleNo, $dbModuleName);
}
echo $moduleHTML;
$sqlstmt->execute();
?>
目前,除了“请选择”选项之外,下拉菜单中没有显示任何内容
更新:
好的,如果您查看 editsessionteacher.php 脚本,它包含以下有效的代码:
$outputmodule = "";
$moduleInfo = explode("_", $_POST['modules']);
$moduleId = $moduleInfo[0];
$moduleName = $moduleInfo[2];
$outputmodule = sprintf("<p><strong>Module:</strong> %s - %s</p>", $moduleId, $moduleName);
module.php 是这样的:
$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 .= sprintf('<option value="%1$s_%2$s">%1$s - %2$s</option>'.PHP_EOL, $dbModuleId, $dbModuleName);
}
echo $moduleHTML;
$sqlstmt->execute();
但是我想将 moduleNo 添加到下拉菜单中,所以当我尝试将代码更改为下面的代码时,它不起作用:
编辑sessionteacher.php:
$outputmodule = "";
$moduleInfo = explode("_", $_POST['modules']);
$moduleId = $moduleInfo[0];
$moduleNo = $moduleInfo[1];
$moduleName = $moduleInfo[2];
$outputmodule = sprintf("<p><strong>Module:</strong> %s - %s</p>", $moduleNo, $moduleName);
模块.php:
$course = isset($_POST['course']) ? $_POST['course'] : '';
$sql = "
SELECT cm.CourseId, cm.ModuleId, c.CourseNo, m.ModuleNo,
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,$dbCourseNo,$dbModuleNo,$dbCourseName,$dbModuleName);
$moduleHTML = "";
$moduleHTML .= '<select name="modules" id="modulesDrop">'.PHP_EOL;
$moduleHTML .= '<option value="">Please Select</option>'.PHP_EOL;
while($sqlstmt->fetch()) {
$moduleHTML .= sprintf('<option value="%1$s">%2$s - %3$s</option>'.PHP_EOL, $dbModuleId, $dbModuleNo, $dbModuleName);
}
$moduleHTML .= '</select>';
echo $moduleHTML;