好的,如果您查看editsessionteacher.php腳本,它包含下面的代碼,該代碼在下拉菜單中顯示下面的值時工作:更改代碼會導致下拉菜單中的選項不出現
$outputmodule = "";
$moduleInfo = explode("_", $_POST['modules']);
$moduleId = $moduleInfo[0];
$moduleName = $moduleInfo[1];
$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加入到下拉菜單,所以當我試圖修改代碼,這在下面,這是行不通的:
editsessionteacher.php:
$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>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post" onsubmit="return validation();">
<table>
<tr>
<th>Course: <?php echo $courseHTML; ?></th>
<th>Module: <?php echo $moduleHTML; ?></th>
</tr>
</table>
<p><input id="moduleSubmit" type="submit" value="Submit Course and Module" name="moduleSubmit" /></p>
</form>
<?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:
$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;
我的問題這就是爲什麼當我改變代碼來添加模塊號時,它不會在下拉菜單中顯示任何內容。
你確定你的查詢將返回任何行?你有沒有嘗試在phpMyAdmin中運行你的查詢?你也可以用'if($ sqlstmt-> num_rows> 0){while($ sqlstmt-> fetch()){$ moduleHTML。= sprintf(''.PHP_EOL,$ dbModuleId,$ dbModuleNo,$ dbModuleName); }} else {echo「NO ROWS RETURNED」;}' – Sean
因此你的''in'modules.php'爲空'0'''? – Sean
我厭倦了迴應$ course變量但沒有輸出,這可能是原因嗎? – user1819709