人的头脑太复杂,时间过得久,有时候连自己也被自己骗了,记下来才是最真实的..
2007年2月13日 10:24
看完辉老大的ajax下拉框联动的例子后,自己弄了一个,hoho~


--------------------------------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[0]!='')      
       document.form1.city.add(new Option(op[0],op[1]));  
    }    
}

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

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[0]!='')          
       document.form1.town.add(new Option(op[0],op[1]));  
    }    
}

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

</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" style="width=100;"></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'].';');
        }
  }
?>
我要 [保存此页] [收藏此页] [复制此链接QQ/Msn发送]
收藏到 [天天] [和讯] [我摘] [天极] [新浪] [博采] [Del.icio.us]
我们推荐您 快去试试吧!
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]