0

我得到了两组我无法弄清楚的错误。一个是我的错误控制台中的 javascript 中出现错误,说明:

Syntax Error: missing ; before statement:
Notice: undefined variable courseDatas in ..... line 28

我得到的另一个错误是 mysqli/php 错误,它指出:

Warning: array_push() expects parameter 1 to be array, ... on line 247

如何消除这两个错误?

Javascript:

<script type="text/javascript">
    $(document).ready( function(){
        var coursedatas = <?php echo json_encode($courseDatas); ?>; //error here
        $('#coursesDrop').change(function()
        {
            var courseId = $(this).val(),
            coursedata;
            for (var i = 0, l = coursedatas.length; i < l; i++)
            {
                if (coursedatas[i].courseId = courseId) { coursedata = coursedatas[i]; }
            }
            var index = $('#newDuration option[value="' + courseData.Duration + '"]').index('#newDuration option');
            $('#newDuration')[0].selectedIndex = index;
        });
    });
</script>

......

PHP:

$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;           

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($courseDatas, $courseData); //error here
}    
$courseHTML .= '</select>';
4

1 回答 1

1

$courseDatas变量需要在您的 while 循环之外定义为一个数组。

已编辑 - 这是您的脚本需要的样子:

$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;
$courseDatas = 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($courseDatas, $courseData); //error here
}
$courseHTML .= '</select>';

<script type="text/javascript">
    $(document).ready( function(){
        var coursedatas = '<?=json_encode($courseDatas);?>';
        //now we have established some data for the $courseDatas array!
        $('#coursesDrop').change(function()
        {
            var courseId = $(this).val(), coursedata;
            for (var i = 0, l = coursedatas.length; i < l; i++)
            {
                if (coursedatas[i].courseId = courseId) { coursedata = coursedatas[i]; }
            }
            var index = $('#newDuration option[value="' + courseData.Duration + '"]').index('#newDuration option');
            $('#newDuration')[0].selectedIndex = index;
        });
    });
</script>
于 2012-12-02T01:48:26.537 回答