<script language="javascript">
function college2major(collegeid)
{
var allcount=0;
majorarray=new array();
<?php
$sql="select * from major";
$result=mysql_query($sql);
$count=0;
while($myrow=mysql_fetch_array($result))
{
?>
majorarray[<?php =$count ?>]=new array(""<?php =$myrow['major_id']?>","<?php =$myrow['major_name'] ?>","<?php =$myrow['college_id'] ?>" );
<?php
$count++;
}
echo "allcount=$count";
?>
document.studentform.major.length=0;
document.studentform.major.options[document.studentform.major.length]=new option("选择专业","");
var collegeid=collegeid;
var i;
for(i=1;i<allcount;i++)
{
if(majorarray[i][2]==collegeid)
{
document.studentform.major.options[document.studentform.major.length]=new option(majorarray[i][1],majorarray[i][0]);
}
}
}function major2class(majorid)
{
var allcount=0;
classarray=new array();
<?php
$sql="select * from class";
$result=mysql_query($sql);
$count=0;
while($myrow=mysql_fetch_array($result))
{
?>
classarray[<?php =$count ?>]=new array(""<?php =$myrow['class_id']?>","<?php =$myrow['class_name'] ?>","<?php =$myrow['major_id'] ?>" );
<?php
$count++;
}
echo "allcount=$count";
?>
document.studentform.class.length=0;
document.studentform.class.options[document.studentform.class.length]=new option("选择班级","");
var majorid=majorid;
var i;
for(i=1;i<allcount;i++)
{
if(classarray[i][2]==majorid)
{
document.studentform.class.options[document.studentform.class.length]=new option(classarray[i][1],classarray[i][0]);
}
}
}
</script>
<select name="college" onchange="college2major(this.options[this.selectedIndex].value)">
<option selected value="">选择学院</option>
<?php
$sql="select * from college";
$result=mysql_query($sql);
while($myrow =mysql_fetch_array($result))
{
?>
<option value="<?php $myrow 1["college_id"];?>">
<?php echo $myrow ["college_name"];?>
</option>
<?php }?>
</select>
<select name="major" id="major" onchange="major2class(this.options[this.selectedIndex].value)">
<option selected value="">选择专业</option>
</select><select name="class"id="class" >
<option selected value="">选择班级</option>
</select>数据库中的college里college_id是主键;major里major_id是主键,college_id是college里的外键;class里的class_id是主键,major_id是major里的外键我咋不能实现关联,哪错了??求解

解决方案 »

  1.   

    上午在论坛发帖刚求教这个问题,把我写的贴出来你参考下吧
    数据表说明:三个字段id、name、parent_id
    可以不连后台,拿测试数据试一下
    后台部分:<?php
    /*
    *说明:联动菜单
    */
    require(xxx);
    $add_result = mysql_query("select * from xxx");
    $add_result_num = mysql_num_rows($add_result);
    echo '<script type="text/javascript">var name_array,parent_array,id_array;name_array=new Array();parent_array=new Array();id_array=new Array();';
    for($i = 0; $i < $add_result_num; $i++){
        mysql_data_seek($add_result,$i);
        $add_result_information = mysql_fetch_array($add_result);
        echo 'id_array['.$i.']='.$add_result_information['xxx'].';';
        echo 'name_array['.$i.']='.$add_result_information['xxx'].';';
        echo 'parent_array['.$i.']='.$add_result_information['xxx'].';';
    }
    echo '</script>';
    ?>前台部分:<html>
    <head>
    <title>测试</title>
    <script type="text/javascript">
    //*测试数据
    var id_array,name_array,parent_array;
    id_array = new Array(1,2,3,4,5,6,7,8,9,10,11,12,13,14);
    name_array = new Array("湖北","广东","黄冈","武汉","东莞","虎门","浠水","蕲春","武昌","汉阳","东一","东二","虎一","虎二");
    parent_array = new Array(0,0,1,1,2,2,3,3,4,4,5,5,6,6);/****三级版****/
    function create_select(parent_id,sel_id,what_sel){
    var new_option;
    if(parent_id === ""){
    return;
    }
    if(what_sel == 1){
    document.getElementById("mid_sel").options.length = 0;
    document.getElementById("bot_sel").options.length = 0;
    new_option = new Option("请选择地区","");
    document.getElementById("mid_sel").options.add(new_option);
    new_option = new Option("请选择县市","");
    document.getElementById("bot_sel").options.add(new_option);
    }
    if(what_sel == 2){
    document.getElementById("bot_sel").options.length = 0;
    new_option = new Option("请选择县市","");
    document.getElementById("bot_sel").options.add(new_option);
    }
    for(var j = 0; j < id_array.length; j++){
    if(parent_array[j] == parent_id){
    new_option = new Option(name_array[j],id_array[j]);
    document.getElementById(sel_id).options.add(new_option);
    }
    }
    }
    /****无限级版****/
    //获取上一级id
    function get_top_id(the_id){
    for(var i = 0; i < id_array.length; i++){
    if(the_id == id_array[i]){
    return parent_array[i];
    break;
    }
    }
    }
    //获取下一级id
    function get_bottom_id(the_id){
    var c_array = new Array();
    var n = 0;
    for(var i = 0; i < parent_array.length; i++){
    if(the_id == parent_array[i]){
    c_array[n] = id_array[i];
    n++;
    }
    }
    if(n > 0){
    return c_array;
    }else{
    return false;
    }
    }
    //生成下拉菜单代码
    function create_sel(the_id){
    the_id = Number(the_id);
    var node_array,option_array,sel_name,head_code,last_code,sel_code;
    document.getElementById("set_sel").innerHTML = '';
    var node_array = new Array();
    var n = 0,loop_go_on = the_id;
    while(loop_go_on != 0){
    node_array[n] = get_top_id(loop_go_on);
    loop_go_on = node_array[n];
    n++;
    }
    node_array.unshift(the_id);
    head_code = '<form method="post" action="">';
    last_code = '</form>';
    sel_code  = '';
    for(var k = node_array.length - 1; k >= 0; k--){
    option_array = get_bottom_id(node_array[k]);
    if(option_array !== false){
    sel_name  = 'select' + node_array[k];
    sel_code += '<select name="' + sel_name + '" onchange="create_sel(this.value)">';
    for(var l = 0; l < option_array.length; l++){
    for(var m = 0; m < id_array.length; m++){
    if(option_array[l] == id_array[m]){
    if(id_array[m] == node_array[k - 1]){
    sel_code += '<option value=' + id_array[m] + ' selected>' + name_array[m] + '</option>';
    }else{
    sel_code += '<option value=' + id_array[m] + '>' + name_array[m] + '</option>';
    }
    break;
    }
    }
    }
    sel_code += '</select>';
    }
    }
    document.getElementById("set_sel").innerHTML = head_code + sel_code + last_code;
    }
    </script>
    </head><body onLoad="create_sel(5)">
    <form name="create_sel" method="post" action="create_sel.php">
        <select id="top_sel" name="top_sel" onChange="create_select(this.value,'mid_sel',1)">
        <option value="">请选择省份</option>
        </select>
        <select id="mid_sel" name="mid_sel" onChange="create_select(this.value,'bot_sel',2)">
        <option value="">请选择地区</option>
        </select>
        <select id="bot_sel" name="bot_sel">
        <option value="">请选择县市</option>
        </select>
        </form>
        <div id="set_sel"></div>
    </body>
    </html>
      

  2.   

    上面代码测试有点问题,两种类别必须分开测试,要一起测试得按下面改一下:<body onLoad="create_select(0,'top_sel',0),create_sel(0)">
    <form name="my_form" method="post" action="create_sel.php">