PHP和Ajax中的从属组合框

时间:2017-01-23 13:41:04

标签: php jquery ajax combobox

我的代码需要帮助。我想根据第一个组合框选择从数据库中选择值。 第一个组合框有主题ID,我想在我的SQL中使用它来查找第二个组合框的值。

<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 }
 }
      ?>

这里在分配编号时,我根本无法查看任何信息。 请帮我。

1 个答案:

答案 0 :(得分:0)

问题出在ajax请求中的参数SubjectID中。试试这个:

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