相关的选择输入

时间:2012-04-24 09:59:30

标签: php javascript html ajax

任何人都可以帮助我。为了显示来自MySql数据库的rezults,我必须选择学校,班级,科目和考试。但列出所有课程或所有考试都不是很实用,所以我想做另一个功能,当我在第一个选择框中选择一些学校时,它会在第二个选择框中仅显示所选学校的课程。

我的代码是:

    <div id="allselects">
<form action="viewing.php" method="post" name="filt">
<div class="multsarrange">
<h1 class="choosetext" >Chose Schools</h1>
<select class="multipleselect"  name="schools[]" size="8" multiple="multiple" id="shkll">
  <?php

                        $sql = "SELECT * FROM schools ";
                        $scc=mysql_query($sql);
                        while ($db_f = mysql_fetch_assoc($scc)) {
                        $schcd=$db_f['schoolcode'];
                        $schc=$db_f['schoolname'];
                        echo "<option  value=$schcd >$schc</option>";
                            } 
                        ?>
  </select>
</div>
<div class="multsarrange" id="clasaajax">
<h1 class="choosetext" >Chose an Classes</h1>
<select class="multipleselect" name="classes[]" size="8" multiple="multiple" ">
<?php

                        $c = "SELECT * FROM classes ";
                        $cl=mysql_query($c);
                        while ($db_f = mysql_fetch_assoc($cl)) {
                        $clsc=$db_f['schoolID'];
                        $claid=$db_f['classID'];
                        $clay=$db_f['year'];
                        $clanm=$db_f['className'];
                        $name=schoolidton($clsc)."  ".$clay." ".$clanm;
                        echo "<option  value=$claid >$name</option>";
    } 
?>
</select>
</div>
<div class="multsarrange">
<h1 class="choosetext" >Chose Subjects</h1>
<select class="multipleselect" name="subjects[]" size="8" multiple="multiple">
<?php

                        $sb = "SELECT * FROM subjects ";
                        $sbi=mysql_query($sb);
                        while ($db_f = mysql_fetch_assoc($sbi)) {
                        $sbnm=$db_f['subjectName'];
                        $sbid=$db_f['subjectID'];
                        echo "<option  value=$sbid >$sbnm</option>";
                            } 
                        ?>
</select>
</div>
<div class="multsarrange">
<h1 class="choosetext" >Chose Exams</h1>
<select class="multipleselect" name="exams[]" size="8" multiple="multiple">
<?php

                        $e = "SELECT * FROM exams ";
                        $ex=mysql_query($e);
                        while ($db_f = mysql_fetch_assoc($ex)) {
                        $id=$db_f['examID'];
                        $sub=$db_f['subjectID'];
                        $desc=$db_f['description'];
                        $year=$db_f['year'];
                        $data=$db_f['data'];

                        $exnam=subidton($sub)." - ".$year." - ".$desc." - ".$data;
                        echo "<option  value=$id >$exnam</option>";
                            } 
                        ?>
</select>
</div>
<div id="longsubmit">
</br></br>
<input name="submit" type="submit" value="View" />
</div>
</form>
</div>

3 个答案:

答案 0 :(得分:1)

您需要做的是:

  1. select上设置一个事件监听器,以监听change事件 - see here
  2. 通过将所选值发送到PHP脚本 - see here
  3. 来处理更改事件
  4. 使用PHP获取所选值并根据需要查询数据库(您已经这样做了)
  5. 如果您只是创建一个新的选择列表,则发送相关的HTML输出或JSON或XML - 这是一个简单的回声
  6. 使用JavaScript将输出写入屏幕 - 这是根据PHP函数的回复创建新元素或插入HTML响应
  7. 这个过程中有很多东西 - 每个都有多个选项 - 我建议你尝试解决每一个问题,如果你遇到困难,请回答具体问题

答案 1 :(得分:0)

在abc.php上使用ajax获取学校的值或ID并制作所需的标签并将结果返回给html的相关id     function get_options(table,id_field,name_field,where_field,field_value,select_id,edit_id){

$('#'+select_id).html('<option>Loading...</option>');
$.ajax({
        type: "POST", url: "abc.php", data: "&table="+table+"&id_field="+id_field+"&name_field="+name_field+"&where_field="+where_field+"&field_value="+field_value+ "&edit_id=" + edit_id +"&get_option=1",
        complete: function(data){
             //alert(data.responseText);
                 $('#'+select_id).html(data.responseText);
        }
    });

}

答案 2 :(得分:0)

你必须使用AJAX来实现这一点。检查一下

http://www.plus2net.com/php_tutorial/ajax_drop_down_list.php