0

我在这里有一个应用程序

现在让我们在我的数据库中说它看起来像这样:

课程表:

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

3 回答 3

2

在您的代码中,您有

if (courseinfo[i].courseId = courseId) {
  coursedata = courseinfo[i];
}

你的if不好,应该是

if (courseinfo[i].courseId == courseId) {
  coursedata = courseinfo[i];
}

然后,要选择,您应该使用

$('#newDuration option[value="' + courseData.Duration +  "]').attr("selected","selected");

您需要删除 $('#newDuration')[0].selectedIndex = index;。你在 for 循环中的比较是 courseId 而你的对象包含 CourseId

于 2012-12-02T23:40:16.643 回答
2

一个问题是:

if (courseinfo[i].courseId = courseId) {
    coursedata = courseinfo[i];
}

应该是这样的:

if (courseinfo[i].courseId == courseId) {
    coursedata = courseinfo[i];
}

您需要比较运算符==而不是=.

另一个问题是以下是文字字符串而不是调用 php 函数:

var courseinfo = '<?php=json_encode($courseInfo);?>';

查看页面源代码,您会看到它courseinfo的值是 ,'<?php=json_encode($courseInfo);?>'而不是输出json_encode($courseInfo)。您需要修复此问题以输出 php.ini 文件。

编辑 2

您的代码中还有一些问题。这是可以工作的代码。

Javascript

var courseinfo = [{"CourseId":1,"CourseNo":"INFO101","CourseName":"Bsc Information Communication Technology","Duration":"3\/4"},{"CourseId":2,"CourseNo":"INFO102","CourseName":"Bsc Computing","Duration":"3\/4"},{"CourseId":8,"CourseNo":"INFO103","CourseName":"Business and Finance","Duration":"3"},{"CourseId":7,"CourseNo":"INFO104","CourseName":"English","Duration":"3\/4"},{"CourseId":9,"CourseNo":"INFO105","CourseName":"Mathematics","Duration":"3\/4"},{"CourseId":10,"CourseNo":"INFO106","CourseName":"Law","Duration":"3"}];

$('#coursesDrop').change(function() {

    var courseId = $(this).val(),
        coursedata, i;

    for (i = 0, l = courseinfo.length; i < l; i++) {
        if (courseinfo[i].CourseId == courseId) {
            coursedata = courseinfo[i];
        }
    }

    var index = $('#newDuration').val(coursedata.Duration);

});​

演示

这就是问题所在。

您需要删除courseinfo数组周围的单引号:

var courseinfo = [...];

您的 if 语句需要正确的大小写来表示它的 variable courseinfo CourseId。在数组中CourseId,这就是if比较中需要的内容:

if (courseinfo[i].CourseId == courseId) {
    ...
}

用于coursedata.Duration设置#newDuration val.

var index = $('#newDuration').val(coursedata.Duration);
于 2012-12-02T23:40:29.020 回答
1

尝试:

$('#newDuration').val(courseData.Duration);

安装的

var index = $('#newDuration option[value="' + courseData.Duration +'"]').index('#newDuration option');
$('#newDuration')[0].selectedIndex = index;

- - 编辑 - -

如果courseinfo是 JsonString 你应该改变你的代码是这样的:

var courseinfoJson = '<?php=json_encode($courseInfo);?>';
var courseinfo = $.parseJSON(courseinfoJson);

 $('#coursesDrop').change(function() {
    var courseId = $(this).val();
    var coursedata;

    for (var i = 0, i = courseinfo.length; i++) {
        if (courseinfo[i]['CourseId'] == courseId) {
            coursedata = courseinfo[i];
        }
    }

    $('#newDuration').val(courseData['Duration']);
}

- - 编辑 - -

于 2012-12-02T23:45:47.307 回答