现在让我们在我的数据库中说它看起来像这样:
课程表:
CourseId CourseNo CourseName Duration
1 INFO101 ICT 3/4
2 INFO102 Computing 2/3
3 INFO103 Computing Science 3
在课程下拉菜单中,它显示数据库中的课程列表,如下所示:
Please Select
INFO101-ICT
INFO102-Computing
INFO103-Computing Science
在持续时间下拉菜单中,它显示以下持续时间选项:
Please Select
1
1/2
2
2/3
3
3/4
4
4/5
5
5/6
6
6/7
7
7/8
8
8/9
9
9/10
10
现在假设在应用程序中发生的是,当您从下拉菜单中选择一门课程时,它应该从持续时间下拉菜单中选择与数据库中属于该课程的持续时间相匹配的持续时间。
因此,例如,如果我从课程INFO101-ICT
下拉菜单中选择课程,那么它应该3/4
在持续时间下拉菜单中自动选择持续时间。
但是在应用程序中它没有这样做,当我选择一门课程时,它只是在持续时间下拉菜单中选择一个空白选项。
我的问题是如何根据从课程下拉菜单中选择的课程在持续时间下拉菜单中选择正确的持续时间?
以下是应用程序的完整代码:
<?php
// connect to the database
include('connect.php');
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
die();
}
?>
<h1>EDIT COURSE</h1>
<?php
$min_year = 1;
$max_year = 10;
$years = range($min_year, $max_year); // returns array with numeric values of 1900 - 2012
$durationHTML = '';
$durationHTML .= '<select name="duration" id="newDuration">' . PHP_EOL;
$durationHTML .= '<option value="">Please Select</option>' . PHP_EOL;
foreach ($years as $year) {
$durationHTML .= "<option>$year</option>" . PHP_EOL;
if ($year != $max_year) {
$nextYear = $year + 1;
$durationHTML .= "<option>$year/$nextYear</option>" . PHP_EOL;
}
}
$durationHTML .= '</select>';
$newCourseNo = (isset($_POST['CourseNoNew'])) ? $_POST['CourseNoNew'] : '';
$coursequery = "
SELECT CourseId, CourseNo, CourseName, Duration
FROM Course
ORDER BY CourseNo
";
$courseqrystmt = $mysqli->prepare($coursequery);
// You only need to call bind_param once
$courseqrystmt->execute();
$courseqrystmt->bind_result($dbCourseId, $dbCourseNo, $dbCourseName, $dbDuration);
$courseqrystmt->store_result();
$coursenum = $courseqrystmt->num_rows();
$courseHTML = '';
$courseHTML = '<select name="courses" id="coursesDrop">' . PHP_EOL;
$courseHTML .= '<option value="">Please Select</option>' . PHP_EOL;
$courseInfo = array();
while ($courseqrystmt->fetch()) {
$courseHTML .= sprintf("<option value='%s'>%s - %s</option>", $dbCourseId, $dbCourseNo, $dbCourseName) . PHP_EOL;
$courseData = array();
$courseData["CourseId"] = $dbCourseId;
$courseData["CourseNo"] = $dbCourseNo;
$courseData["CourseName"] = $dbCourseName;
$courseData["Duration"] = $dbDuration;
array_push($courseInfo, $courseData);
}
$courseHTML .= '</select>';
$courseform = "
<form action='" . htmlentities($_SERVER['PHP_SELF']) . "' method='post' id='courseForm'>
<p><strong>Course:</strong> {$courseHTML} </p>
</form>";
echo $courseform;
$editcourse = "
<form id='updateCourseForm'>
<p><strong>New Course Details:</strong></p>
<table>
<tr>
<th>Duration (Years):</th>
<td id='data'>{$durationHTML}</td>
</tr>
</table>
</form>
";
echo $editcourse;
?>
< script type = "text/javascript" >
$(document).ready(function() {
var courseinfo = '<?php echo json_encode($courseInfo);?>';
$('#coursesDrop').change(function() {
var courseId = $(this).val(),
coursedata;
for (var i = 0, l = courseinfo.length; i < l; i++) {
if (courseinfo[i].CourseId == courseId) {
coursedata = courseinfo[i];
}
}
var index = $('#newDuration option[value="' + courseinfo.Duration + '"]').attr('selected','selected');
alert(courseinfo.Duration);
});
});
< /script>