丢失时接收语法错误;在javascript函数之前声明和接收引用错误

时间:2012-12-05 03:17:36

标签: php javascript jquery html

嗨,当我打开应用程序时(代码是问题的底部),马上我得到一个错误说明:

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>

1 个答案:

答案 0 :(得分:5)

删除php行中的空格:

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

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

<? php应该像<?php

一样