0

嗨,当我打开应用程序时(代码在问题的底部),我立即收到一条错误消息:

SyntaxError:missing ; before statement

上述错误在查看页面源代码中显示了这一行:

var moduleinfo = 
Notice: Undefined variable: moduleInfo in ... on line 267

如何修复上述错误?

同样在下面的代码中,当用户从课程下拉菜单中选择课程并提交课程时,它会显示一个模块下拉菜单,但它也给了我另一个错误说明:

ReferenceError: validation is not defined

上面的错误表明这个错误在页面源代码的第 1 行,它只是一个空行。

我的问题是如何在下面的代码中修复这两个错误:(下面代码的顺序是应用程序中的确切顺序,先是 php,然后是 javascript,我还评论了第 267 行的位置(javascript ) 对于第一个错误并注释了 javascript 函数验证的位置)

PHP/HTML

<?php

// connect to the database
include('connect.php');
include('noscript.php');

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    die();
}


$sql = "SELECT CourseId, CourseNo, CourseName FROM Course ORDER BY CourseNo";

$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">' . PHP_EOL;
$courseHTML .= '<option value="">Please Select</option>' . PHP_EOL;

$outputcourse = "";
$hiddencourse = "";

while ($sqlstmt->fetch()) {
    $course     = $dbCourseId;
    $courseno   = $dbCourseNo;
    $coursename = $dbCourseName;
    $courseHTML .= "<option value='" . $course . "'>" . $courseno . " - " . $coursename . "</option>" . PHP_EOL;

    if (isset($_POST['courses']) && ($_POST['courses'] == $course)) {
        $outputcourse .= "<p><strong>Course:</strong> " . $courseno . " - " . $coursename . "</p>";
        $hiddencourse .= "<p><input type='hidden' id='hiddencourse' value='" . $courseno . " - " . $coursename . "'></p>";
    }

}

$courseHTML .= '</select>';


?>

<form action="<?php
echo htmlentities($_SERVER['PHP_SELF']);
?>" method="post" onsubmit="return validation();">
<table>
<tr>
<th>Course: <?php
echo $courseHTML;
?></th>
</tr>
</table>
<p><input id="courseSubmit" type="submit" value="Submit Course" name="courseSubmit" /></p>
<div id="courseAlert"></div>
<div id="targetdiv"></div>
</form>

<?php

if (isset($_POST['courseSubmit'])) {
    //get the form data 
    $coursesdrop = (isset($_POST['courses'])) ? $_POST['courses'] : '';

    $modulequery = "
SELECT
m.ModuleId, m.ModuleNo, m.ModuleName, m.Credits
FROM
Module m
WHERE
m.ModuleId NOT IN (
SELECT cm.ModuleId
FROM Course_Module cm
WHERE cm.CourseId = ?
)
ORDER BY m.ModuleNo
";

    $moduleqrystmt = $mysqli->prepare($modulequery);
    // You only need to call bind_param once
    $moduleqrystmt->bind_param("i", $coursesdrop);
    // get result and assign variables (prefix with db)

    $moduleqrystmt->execute();

    $moduleqrystmt->bind_result($dbModuleId, $dbModuleNo, $dbModuleName, $dbCredits);

    $moduleqrystmt->store_result();

    $modulenum = $moduleqrystmt->num_rows();

    $moduleHTML = '<select name="module" id="modulesDrop">' . PHP_EOL;
    $moduleHTML .= '<option value="">Please Select</option>' . PHP_EOL;

    $moduleInfo = array();

    while ($moduleqrystmt->fetch()) {
        $moduleHTML .= sprintf("<option value='%s'>%s - %s</option>", $dbModuleId, $dbModuleNo, $dbModuleName) . PHP_EOL;

        $moduleData               = array();
        $moduleData["ModuleId"]   = $dbModuleId;
        $moduleData["ModuleNo"]   = $dbModuleNo;
        $moduleData["ModuleName"] = $dbModuleName;
        $moduleData["Credits"]    = $dbCredits;

        array_push($moduleInfo, $moduleData);

    }


    $moduleHTML .= '</select>';


    $moduleexist = "
<div id='rt-container'>
<form id='moduleExistForm'>
<p><strong>Current Modules</strong></p>
<p>{$moduleSELECT}</p>
</form> 
</div>";

    echo $moduleexist;


}

?>

Javascript

<script type="text/javascript">
    $(document).ready(function () {

        var moduleinfo = <?php echo json_encode($moduleInfo); ?> ; //line 267

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

            var moduleId = $(this).val(),
                moduledata;

            for (var i = 0, l = moduleinfo.length; i < l; i++) {
                if (moduleinfo[i].ModuleId == moduleId) {
                    moduledata = moduleinfo[i];
                }
            }

            var currentindex = $('#credits').val(moduledata.Credits);

        });


    });

//below is the validation() function

    function validation() {

        var isDataValid = true;

        var courseTextO = document.getElementById("coursesDrop");

        var errCourseMsgO = document.getElementById("courseAlert");

        if (courseTextO.value == "") {
            $('#targetdiv').hide();
            $('#moduleForm').hide();
            $('#detailsForm').hide();
            $('#addbtn').hide();
            errCourseMsgO.innerHTML = "Please Select a Course";
            isDataValid = false;
        } else {
            errCourseMsgO.innerHTML = "";
        }

        return isDataValid;

    }
</script>
4

1 回答 1

5

删除 php 行中的空格:

var moduleinfo = <? php echo json_encode($moduleInfo); ?> ;

var moduleinfo = <?php echo json_encode($moduleInfo); ?> ;

<? php应该在一起就好<?php

于 2012-12-05T03:20:35.037 回答