2012-04-11 7 views
3

phpとajaxを使用してdbからデータを取り込む2つのドロップダウンボックスを作ろうとしています。最初のドロップダウンボックスには1つの選択肢しかありませんが、選択すると2つ目のドロップダウンボックスに私の学校の異なる部門がデータベースから取得されます。部門を選択すると、その部門のすべてのコースが3番目のドロップダウンボックスに表示されます。今は、すべての部門が配置されるドロップダウンボックスを取得しようとしていますが、クリックするとすぐに何も入力されません。ドロップダウンボックスに部門の名前を書き込むループに問題があると思います。ここに私のコードはdbからプルダウンするカスケードドロップダウンボックス

の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

あなたが欠落している引用符を持っている:findDept.phpが呼び出さなっている