2012-04-11 83 views
3

我試圖做兩個下拉框從數據庫使用php和ajax填充。第一個下拉框只有一個選擇,但是當您選擇它時,它應該填充第二個下拉框,其中來自我的學校的不同部門從數據庫中拉出。在選擇部門時,應該在第三個下拉框中顯示該部門的所有課程。現在我只是試圖讓所有部門的下拉框填充,當你點擊它時,它不會填充任何東西。我認爲循環在下拉框中寫入部門名稱存在問題。這裏是我的代碼級聯下拉框拉從分區

的index.php

<html> 
    <head> 
     <meta http-equiv="Content-Type" 
      content="text/html; charset=iso-8859-1"> 
     <script language="javascript" type="text/javascript"> 

      function getXMLHTTP() { //function to return the xml http object 
       var xmlhttp=false;  
       try{ 
        xmlhttp=new XMLHttpRequest(); 
       } 
       catch(e) {   
        try{    
         xmlhttp= new ActiveXObject("Microsoft.XMLHTTP"); 
        } 
        catch(e){ 
         try{ 
          xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); 
         } 
         catch(e1){ 
          xmlhttp=false; 
         } 
        } 
       } 

       return xmlhttp; 
      } 

      function getDept() {   

       var strURL="findDept.php"; 
       var req = getXMLHTTP(); 

       if (req) { 

        req.onreadystatechange = function() { 
         if (req.readyState == 4) { 
          // only if "OK" 
          if (req.status == 200) {       
           document. 
            getElementById("deptdiv"). 
             innerHTML=req.responseText; 
          } else { 
           alert("There was a problem " + 
            "while using XMLHTTP:\n" + req.statusText); 
          } 
         }     
        }    
        req.open("GET", strURL, true); 
        req.send(null); 
       }   
      } 
      function getCourse(deptId) {   
      var strURL="findCourse.php?dept="+deptId; 
      var req = getXMLHTTP(); 

      if (req) { 
       req.onreadystatechange = function() { 
         if (req.readyState == 4) { 
          // only if "OK" 
          if (req.status == 200) {       
           document. 
            getElementById('coursediv'). 
             innerHTML=req.responseText; 
          } else { 
           alert("There was a problem " + 
            "while using XMLHTTP:\n" + req.statusText); 
          } 
         }     
        }    
        req.open("GET", strURL, true); 
        req.send(null); 
       } 
      } 
     </script> 
    </head> 
    <body bgcolor= 'red' > 
     <p> 
      <font size="8" face="arial" 
       color="black">WELCOME TO SCHEDULE BUILDER</font> 
     </p> 
     <form method="post" action="" name="form1"> 
      <table width="60%" border="0" cellspacing="0" cellpadding="0"> 
       <tr> 
        <td width = "150">Semester</td> 
        <td width="150"> 
         <select name="semester onChange="getDept()"> 
          <option value="">Select Semester</option> 
          <option value ="1">Spring 2012</option> 
         </select> 
        </td> 
       </tr> 
       <tr style=""> 
        <td>Department</td> 
        <td > 
         <div id="deptdiv"> 
          <select name="department"> 
           <option>Select Department</option> 
          </select> 
         </div> 
        </td> 
       </tr> 
       <tr style=""> 
        <td>Course</td> 
        <td > 
         <div id="coursediv"> 
          <select name="course"> 
           <option>Select Department First</option> 
          </select> 
         </div> 
        </td> 
       </tr> 
       <tr> 
       <td>&nbsp;</td> 
       <td>&nbsp;</td> 
       </tr> 
       <tr> 
       <td>&nbsp;</td> 
       <td>&nbsp;</td> 
       </tr> 
      </table> 
     </form> 
    </body> 
</html> 

findDept.php

<? 
    $link = mysql_connect("sql2.njit.edu", "sk442_proj", "jZ1MOA0X"); 
    if (!link) { 
     die('Could not connect: ' . mysql_error()); 
    } 
    mysql_select_db("sk442_proj"); 
    $query="SELECT abbrev FROM department"; 
    $result=mysql_query($query); 
?> 
<select name="department" onchange="getCourse(this.value)"> 
    <option>Select Department</option> 
<? while($row=mysql_fetch_array($result)) { ?> 
    <option value=$row['abbrev']><?=$row['abbrev']?></option> 
<? } ?> 
</select> 
+0

你有一個缺少的引號: