我得到了两组我无法弄清楚的错误。一个是我的错误控制台中的 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>';