2017-01-23 83 views
0

我需要我的代碼幫助。我想根據第一個組合框選擇從數據庫中選擇值。 第一個組合框有主題ID,我想在我的SQL中使用它來查找第二個組合框的值。PHP和Ajax中的依賴組合框

<form class ="form_group" id="form1" enctype="multipart/form-data" method="POST" action="uploadstudent.php"> 
    StudentID:<br> 
    <input type="text" name="StudentID" value="<?php echo $_SESSION['login_user'];?>"readonly> 
    <br><br> 
    Subject ID:<br> 
    <select id="soflow" name="SubjectID" onChange="getState(this.value);"> 
    <option> 
    <?php 
    mysql_connect("localhost", "id503120_course", "12345678"); 
    mysql_select_db('id503120_course_db'); 
     $StudentID=$_SESSION['login_user']; 
     $Course = mysql_query("SELECT course FROM student WHERE StudentID = '".$StudentID."'"); 
$result = mysql_fetch_assoc($Course); 
$newcourse = implode($result); 
     $query=mysql_query("SELECT SubjectID FROM subjects WHERE Course= '".$newcourse."' AND SubStatus = 'Y'");    
     if(!$numrows=mysql_num_rows($query)==0) 
     { 
      while($row=mysql_fetch_assoc($query)) 
      { ?> 
       <option value="<?php echo $row['SubjectID']; ?>"> 
       <?php echo $row['SubjectID']; ?> 
       </option> 
      <?php } 
     } 
     else{ 
      echo "No submissions are currently open for you"; 
     } 

     ?> 
    </select> 
<script> 
function getState(val) { 
    $.ajax({ 
    type: "POST", 
    url: "submission.php", 
    data:'SubjectID='+val, 
    success: function(data){ 
     $('#soflow2').html(data); 
    } 
    }); 
} 
</script> 


    Assign number: <br><select id="soflow2" name="AssessmentNum"> 
    <option> 
    <?php 
    mysql_connect("localhost", "id503120_course", "12345678"); 
    mysql_select_db('id503120_course_db'); 

if(!empty($_POST["SubjectID"])) { 
    $query ="SELECT AssignNum FROM subjects WHERE SubjectID = '".$_POST['SubjectID']."'"; 
       while($row=mysql_fetch_assoc($query)) 
      { ?> 
       <option value="<?php echo $row['AssignNum']; ?>"> 
       <?php echo $row['AssignNum']; ?> 
       </option> 
      <?php } 
} 
     ?> 

在這裏分配號碼,我根本無法查看任何信息。 請幫幫我。

+0

PS我的文件名爲submission.php –

回答

0

問題出在ajax請求中的參數SubjectID中。試試這個:

function getState(val) { 
    $.ajax({ 
    type: "POST", 
    url: "submission.php", 
    data:{'SubjectID' :val}, 
    success: function(data){ 
     $('#soflow2').html(data); 
    } 
    }); 
} 
+0

感謝您的幫助! –

+0

我設法通過改變我的方法來做到這一點。 –