kinght2008 发表于 2015-8-24 16:50:39

php+ajax实现市县下拉框联动的简单代码

--------------------------------html------------------------------------------------------
<html>
<head>
<meta http-equiv="Content-Type" c>
<title>无标题文档</title>
<script language="javascript">
var ajax=false;
function initAjax()
{
ajax = false;
if(window.XMLHttpRequest)
   { ajax = new XMLHttpRequest();}
else
   if (window.ActiveXObject)
      {
         try {ajax = new ActiveXObject("Msxml2.XMLHTTP"); }
         catch (e)
            {
             try {ajax = new ActiveXObject("Microsoft.XMLHTTP");}
             catch (e) {}
            }
      }
if (!ajax)
{
    window.alert("不能创建XMLHttpRequest对象实例");
    return false;
}   
}
//-------------
function fill()
{
      initAjax();
      ajax.onreadystatechange=fillcity;
ajax.open("get","test.php?cityid=all",true);
ajax.send(null);      
}

function fillcity()
{
if(ajax.readyState==4)
   if(ajax.status==200)
       {splitcity(ajax.responseText); }
   else
      {alert('no data come back');}      
}
function splitcity(rs)
{
document.form1.city.length=0;
var field=rs.split(';');
for(var i=0;i<field.length;i++)
   {
      var subfield=field;
      var op=subfield.split(",");
      if(op!='')      
      document.form1.city.add(new Option(op,op));
   }   
}

//--------------

function changetown()
{
      initAjax();
      ajax.onreadystatechange=filltown;
      var myurl="test.php?cityid="+document.form1.city.value;
ajax.open("get",myurl,true);
ajax.send(null);      
}

function filltown()
{
var ob='town';
if(ajax.readyState==4)
   if(ajax.status==200)
       {splittown(ajax.responseText); }
   else
       {alert('no data come back');}      
}

function splittown(rs)
{
document.form1.town.length=0;
var field=rs.split(';');
for(var i=0;i<field.length;i++)
   {
      var subfield=field;
      var op=subfield.split(",");
      if(op!='')         
      document.form1.town.add(new Option(op,op));
   }   
}

//-------------

</script>
</head>

<body>
<form name="form1" action="" method="get">
<table width="200" border="0" cellspacing="0" cellpadding="0">
    <tr>
      <td height="20">City:<select name="city"   style="width=100;" ></select></td>
    </tr>
    <tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr>
    <tr>
      <td height="20">Town:<select name="town"></select></td>
    </tr>   
</table>
</form>
</body>
</html>

---------------------------------------------php--------------------------------------------------
<?php
header('Content-Type:text/html;charset=GB2312');
include_once("conn.php");//数据库连接
$id=$_GET['cityid'];
if($id=='all')
   {
       $sql="select id,cityname from city";
       $query=mysql_query($sql);
       if(@mysql_num_rows($query)>0)
       {
         while ( $row = mysql_fetch_array($query))      
             echo( $row['cityname'].','.$row['id'].';' );      
       }

    }
else   
   {
   if(empty($id))
       $sql="select id ,townname from town ";
   else
       $sql="select townname,id from town where cityid=".$id;
   $query=mysql_query($sql);
   if(mysql_num_rows($query)>0)
      {
          while ( $row = mysql_fetch_array($query))
               print($row['townname'].','.$row['id'].';');
         }
   }
?>
页: [1]
查看完整版本: php+ajax实现市县下拉框联动的简单代码