2015-05-19 33 views
0

我正在加载使用ajax的PHP。起初页面加载没有错误。当我触发按钮上的onclick事件时,出现错误“未捕获的SyntaxError:意外的标识符”,它在PHP文件的第二行中指出,这是HTML头标记。未捕获的SyntaxError:意外标识符<head>

php文件:

<html> 
<head> 
    <title>Staff Portal</title> 
    <link rel="stylesheet" type="text/css" href="Instructor.css"> 
    <script type="text/javascript" src="InstLessons.js"></script> 
</head> 
<body onload="loadCourse()"> 
    <div id="InstHeader"> 
    <div id="bar"> 
     <table id="inst_bar_table"> 
     <tbody><tr> 
     <td id="inst_bar_detail"><a href="InstOptions.php">Options</a></td> 
     <td id="inst_bar_detail"><a href="logout.php">Logout</a></td><td> 
     </td></tr> 
     </tbody></table> 
    </div><br> 
    <h1 id="InstHeaderTitle">Pennco Tech Instructor Portal</h1><h1> 
    </h1> 
    </div> 

    <div id="MainBody"> 
    <div id="course"><table id="InstClassTable"><tbody><tr><th id="classHeadTbl">Code</th><th id="classHeadTbl">Course</th><th id="classHeadTbl"># Students</th><th id="classHeadTbl">Start Date</th><th id="classHeadTbl">End Date</th></tr><tr><td id="classHeadDetailTbl">GEN 101</td><td id="classHeadDetailTbl">Computer Applications</td><td id="classHeadDetailTbl">7</td><td id="classHeadDetailTbl">05-06-15</td><td id="classHeadDetailTbl">07-07-15</td></tr></tbody></table></div> 
    <div id="detailsection"> 
     <div id="InstMenu"> 
      <ul id="InstMenuList"> 
      <li><a href="InstMain.php" id="MenuLink">Select Class</a></li> 
      <li><a href="InstAttendance.php" id="MenuLink">Class Attendance</a></li> 
      <li><a href="InstClassAssignments.php" id="MenuLink">Class Assignments</a></li> 
      <li><a href="InstClassMsgboard.php" id="MenuLink">Message Board</a></li> 
      <li><a href="InstClassStudentData.php" id="MenuLink">Student Data</a></li> 
      <li><a href="InstClassOverView.php" id="MenuLink">Class Summary</a></li> 
      </ul>   
     </div> 
    <div id="instLessons"> 
    <h3><u>Course Lessons</u></h3> 
    <br> 
     <table id="instLessonTable"> 
      <tbody> 
      <tr> 
       <td id="classHeadDetailTbl">Career Services Assignments</td> 
       <td id="InstLessonButtonDetail"> 
        <button id="InstLessonButton" onclick="getLessons(Career Services Assignments)">Select</button> 
       </td> 
       </tr> 
       <tr><td id="classHeadDetailTbl">Mitchell Final</td> 
       <td id="InstLessonButtonDetail"><button id="InstLessonButton" onclick="getLessons(Mitchell Final)">Select</button></td> 
       </tr> 
       <tr> 
       <td id="classHeadDetailTbl">Mitchell Hand-In Assignments</td> 
       <td id="InstLessonButtonDetail"> 
        <button id="InstLessonButton" onclick="getLessons(Mitchell Hand-In Assignments)">Select</button></td> 
       </tr> 
       <tr> 
       <td id="classHeadDetailTbl">Mitchell Lab Test</td> 
       <td id="InstLessonButtonDetail"> 
       <button id="InstLessonButton" onclick="getLessons(Mitchell Lab Test)">Select</button> 
       </td> 
       </tr> 
       <tr> 
       <td id="classHeadDetailTbl">Mitchell Midterm</td> 
       <td id="InstLessonButtonDetail"> 
        <button id="InstLessonButton" onclick="getLessons(Mitchell Midterm)">Select</button> 
       </td> 
       </tr> 
       <tr> 
       <td id="classHeadDetailTbl">PC</td> 
       <td id="InstLessonButtonDetail"> 
       <button id="InstLessonButton" onclick="getLessons(PC)">Select</button> 
       </td> 
       </tr> 
      </tbody> 
      </table> 
     </div> 
     <div id="instLessonMenu"> 
     <br> 
     <button id="LessonMenuButton" onclick="SetPercentages()">Set Course Percentages</button><br><br> 
     <button id="LessonMenuButton" onclick="newLesson()">New Class Lesson</button><br><br> 
     <button id="LessonMenuButton" onclick="DelLesson()">Delete Class Lesson</button><br><br> 
     <button id="LessonMenuButton" onclick="IndivLesson()">New Individual Lesson</button><br><br> 
     <button id="LessonMenuButton" onclick="DelIndivLesson()">Delete Individual Lesson</button><br><br> 
     </div> 
    </div> 
    </div> 

按钮onclick事件JavaScript函数:

function getLessons(lesson){ 
    if(xmlHttpGetLessons.readyState==4 || xmlHttpGetLessons.readyState == 0){ 
     var lesson_title = encodeURIComponent(lesson); 
     xmlHttpGetLessons.open("GET", 'InstListLessons.php?lesson ="' + lesson_title + '"',true); 
     xmlHttpGetLessons.send(null); 
     xmlHttpGetLessons.onreadystatechange = LessonGetServerResponse; 
    } 
} 
+0

你的PHP文件看起来很像一个HTML文件? –

+0

哈哈,是的,它的确如此。我打算以后再添加更多内容,但首先需要达到这一点。 – Gene

+0

尝试用'<?php echo“Hello World!替换一个简单的'.php'文件。 ?>看看你是否遇到错误? –

回答

0

你的JavaScript函数调用将导致Unexpected identifier语法错误。

<button … onclick="getLessons(Career Services Assignments)"> 

单击按钮时,会执行以下JavaScript并导致错误。

getLessons(Career Services Assignments) 

你会想让函数参数成为一个字符串,就像你通常在JavaScript中一样;通过用引号字符包装内容。 (单引号选择,因为我们将嵌入该呼叫转变为HTML属性值,它使用双引号)。

getLessons('Career Services Assignments') 

啪这回您的HTML:

<button … onclick="getLessons('Career Services Assignments')"> 
+0

谢谢你。添加单引号可以纠正问题。任何想法为什么错误是在文件的第2行显示? – Gene

相关问题