php 三级联动菜单
已测(表结构:id 表ID(唯一)title 各类标题flid 类别的ID (大类为1 中类为2 小类为3)pid 上类的ID(大类就跟大类,提交中类的时候这地方写大类的ID,提交小类的时候写中类的ID) )
<?php $link=php_connect("localhost","root","root") or die("php服务器连接错误".mysql_error()); mysql_select_db("sanji",$link) or die("数据库访问错误".mysql_error()); mysql_query("set character set gb2312"); mysql_query("set names gb2312");?><html><head><title>下拉框连动</title></head><body><script language="JavaScript"><!--var subcat = new Array();<?$i=0;$sql="select * from sanji where flid=2";$query=mysql_query($sql,$link);while($arr=mysql_fetch_array($query)){echo "subcat[".$i++."] = new Array(".$arr["pid"].",".$arr["title"].",".$arr["id"].");n";}?>var subcat2 = new Array();<?$i=0;$sql="select * from sanji where flid=3";$query=mysql_query($sql,$link);while($arr=mysql_fetch_array($query)){echo "subcat2[".$i++."] = new Array(".$arr["pid"].",".$arr["title"].",".$arr["id"].");n";}?>function changeselect1(locationid){document.form1.s2.length = 0;document.form1.s2.options[0] = new Option(==请选择==,);for (i=0; i<subcat.length; i++){if (subcat[i][0] == locationid){document.form1.s2.options[document.form1.s2.length] = new Option(subcat[i][1], subcat[i][2]);}}}function changeselect2(locationid){document.form1.s3.length = 0;document.form1.s3.options[0] = new Option(==请选择==,);for (i=0; i<subcat2.length; i++){if (subcat2[i][0] == locationid){document.form1.s3.options[document.form1.s3.length] = new Option(subcat2[i][1], subcat2[i][2]);}}}//--></script>三级联动:<BR><form name="form1"><select name="s1" onChange="changeselect1(this.value)"><option>==请选择==</option><?$sql="select * from sanji where flid=1";$query=mysql_query($sql,$link);while($arr=mysql_fetch_array($query)){echo "<option value=".$arr["id"].">".$arr["title"]."</option>n";}?></select><select name="s2" onChange="changeselect2(this.value)"><option>==请选择==</option></select><select name="s3" onChange="alert(选选择+this.value)"><option>==请选择==</option></select></form><BR></body></html>
数据库叫"sanji"-- phpMyAdmin SQL Dump-- version 2.11.4-- http://www.phpmyadminphp---- 主机: localhost-- 生成日期: 2009 年 11 月 03 日 15:12-- 服务器版本: 5.0.51-- PHP 版本: 5.2.5SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";---- 数据库: `sanji`---- ------------------------------------------------------------ 表的结构 `sanji`--CREATE TABLE IF NOT EXISTS `sanji` ( `id` int(10) NOT NULL auto_increment, `title` varchar(30) collate utf8_unicode_ci NOT NULL, `flid` int(10) NOT NULL, `pid` int(10) NOT NULL, PRIMARY KEY (`id`)) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=5 ;---- 导出表中的数据 `sanji`--INSERT INTO `sanji` (`id`, `title`, `flid`, `pid`) VALUES(1, 我是1, 1, 1),(2, 我是2,归1管, 2, 1),(3, 我是3,归2管, 3, 2),(4, 我是4,也归2管, 3, 2);
本文地址:http://www.phprm.com/frame/php1004984.html
转载随意,但请附上文章地址:-)