自己写的一个,给你吧
分两个版本,三级和无限级的,测试都没问题,数据库相关你改一下就行了
<?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)"><option value="">请选择</option>';
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_select(0,'top_sel',0),create_sel(0)">
<form name="my_form" 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>

解决方案 »

  1.   


      //getcity.php
    <?php 
        include ("connDB.php");
        header("Cache-Control: no-cache, must-revalidate");
        header("Content-type: text/html;charset=utf8"); 
        
       $pro=$_GET["p"];
      
       $SQL="SELECT city FROM cities WHERE province='$pro'";
       print("SQL is: ".$SQL);
       mysql_query("set names utf8");
       $result=mysql_query($SQL);
       $rows=mysql_num_rows($result);
       $cities="<select id=cities onchange=getTowns(this.value)>";
       for($i=0;$i<$rows;$i++){
           mysql_data_seek($result,$i);
           $data=mysql_fetch_array($result);
           $cities.="<option value=$data[0]>$data[0]";
       }
       $cities.="</select>";
       echo $cities;?>
    //getTowm.php
    <?php 
      include ("connDB.php");
      header("Cache-Control: no-cache, must-revalidate");
      header("Content-type: text/html;charset=utf8"); 
     
       
       // �Դ���4�IJ�������ж���֤�� ʡ�ݻ��� ����;
      $url_string=parse_url($_SERVER["REQUEST_URI"]);
      $query_string=$url_string["query"];
      $parameter_string=explode('=',$query_string);
      $parameter_type=$parameter_string[0];            // province �� city
      $parameter_value=$parameter_string[1];           // ��Ӧ��ֵ p ��  c
      
      // ����4����ʡ�� p: [ѡ��ʡ��ֱ�Ӱѵ�һ����е�����ѡ��:
      if($parameter_type=="p"){
        $getCity_SQL="SELECT city FROM cities WHERE province='$parameter_value'limit 1";
        //print("SQL is: ".$getCity_SQL);
        mysql_query("set names utf8");
        $result=mysql_query($getCity_SQL);
        $resultset=mysql_fetch_row($result);
        $rows=mysql_num_rows($result);
        $defalutCity=$resultset[0];
        $SQL="SELECT town FROM towns WHERE city='$defalutCity'";
      }else{  
      // ���� c:
        $SQL="SELECT town FROM towns WHERE city='$parameter_value'";
      }
       //print("SQL is: ".$SQL);
       mysql_query("set names utf8");
       $result=mysql_query($SQL);
       $rows=mysql_num_rows($result);
       $cities="<select id=towns onchange=go()>";
       for($i=0;$i<$rows;$i++){
           mysql_data_seek($result,$i);
           $data=mysql_fetch_array($result);
           $cities.="<option value=$data[0]>$data[0]";
       }
       $cities.="</select>";
       echo $cities;?><!--
    //procity.php 
    --><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>AJAX 實現省市級聯</title>
    <script type="text/javascript">//
    var xmlHttp;          // ���� XMLHttpRequest:
    function createXmlHttpRequest(){
      var xmlHttp=null;
      try{
        // Firefox, Opera 8.0+, Safari
        xmlHttp=new XMLHttpRequest();
      }catch(e){
        // Internet Explorer
        try{
          xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        }catch(e){
          xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
         }
       }
       return xmlHttp;
    }// function getCity(pro){
      xmlHttp=createXmlHttpRequest();
      var url="getCity.php?p="+pro;
      //alert("URL is: "+url);
      xmlHttp.open("GET",url,true); 
      xmlHttp.send(null);
      xmlHttp.onreadystatechange=function(){
     if(xmlHttp.readyState==4 && xmlHttp.status==200){
       document.getElementById('cityList').innerHTML=xmlHttp.responseText;
       // 
       var url="getTown.php?p="+pro;
       //alert("URL is: "+url);
       xmlHttp.open("GET",url,true); 
       xmlHttp.send(null);
       xmlHttp.onreadystatechange=function(){
       if(xmlHttp.readyState==4 && xmlHttp.status==200){
       document.getElementById('townList').innerHTML=xmlHttp.responseText;
       // Display:
       go();
         }
       }
      }
     }
    }//function getTowns(city){
      xmlHttp=createXmlHttpRequest();
      var url="getTown.php?c="+city;
      //alert("URL is: "+url);
      xmlHttp.open("GET",url,true); 
      xmlHttp.send(null);     
      xmlHttp.onreadystatechange=function(){
      if(xmlHttp.readyState==4 && xmlHttp.status==200){
       document.getElementById('townList').innerHTML=xmlHttp.responseText;
       // Display:
        go();
         }
       }
    }// 
    function go(){
       var pro=document.getElementById('provinces').value;
       var city=document.getElementById('cities').value;
       var town=document.getElementById('towns').value;
       document.getElementById('info').innerHTML="你所在的地方:"+pro+"->"+city+"->"+town;
    }</script>
    <?php  include ("connDB.php");
      
      //
      $SQL="SELECT province FROM provinces ORDER BY province";
      mysql_query("set names utf8");
      $result=mysql_query($SQL) or die(mysql_error());
      $rows=mysql_num_rows($result);
      
    ?>
    </head>
    <body onLoad=getCity('北京')>
     AJAX實現省市級聯<HR>
     省(直轄市) <select id=provinces onChange=getCity(this.value)>
          <?php 
             for($i=0;$i<$rows;$i++){
                mysql_data_seek($result,$i);
                $data=mysql_fetch_array($result);
               print("<option value=$data[0]>$data[0]");
             }
          ?>
        </select>
        
    市 <span id=cityList></span>&nbsp縣/區<span id=townList></span><BR><BR>
      <div id=info></div>
    </body>
    </html>
      

  2.   

    自己写的一个,给你吧
    分两个版本,三级和无限级的,测试都没问题,数据库相关你改一下就行了
    -----------------------这里面的XXX是代表什么的?
      

  3.   

    xxx是你放数据的表名称,php部分代码你根据你的实际情况做一些改动,无非是表名 字段名<?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++){
        $add_result_information = mysql_fetch_array($add_result);
        echo 'id_array['.$i.']='.$add_result_information['xxx'].';';//这里填id字段名称
        echo 'name_array['.$i.']='.$add_result_information['xxx'].';';//这里填名称字段名称
        echo 'parent_array['.$i.']='.$add_result_information['xxx'].';';//这里填父级字段名称
    }
    echo '</script>';
    ?>
      

  4.   

      <script type="text/javascript">var name_array,parent_array,id_array;name_array=new Array();parent_array=new Array();id_array=new Array();id_array[0]=4;name_array[0]=EVO;parent_array[0]=3;id_array[1]=13;name_array[1]=MITSUBISHI;parent_array[1]=0;id_array[2]=5;name_array[2]=2005;parent_array[2]=4;id_array[3]=6;name_array[3]=2008;parent_array[3]=4;id_array[4]=12;name_array[4]=Toyota;parent_array[4]=0;id_array[5]=8;name_array[5]=FT86;parent_array[5]=7;id_array[6]=9;name_array[6]=GT86;parent_array[6]=7;id_array[7]=10;name_array[7]=2013;parent_array[7]=9;id_array[8]=11;name_array[8]=2009;parent_array[8]=8;id_array[9]=14;name_array[9]=GT86;parent_array[9]=12;id_array[10]=15;name_array[10]=EVO;parent_array[10]=13;id_array[11]=16;name_array[11]=carola;parent_array[11]=12;id_array[12]=18;name_array[12]=20135;parent_array[12]=15;id_array[13]=20;name_array[13]=5552;parent_array[13]=14;id_array[14]=21;name_array[14]=33655;parent_array[14]=16;id_array[15]=23;name_array[15]=2005;parent_array[15]=15;</script>这个是 数据库读出来以后,貌似不行也,无法载入联动