谁会用php制作三级联动目录 目录保存在mysql数据库里
展开全部
<?
$link=mysql_connect("localhost","root","123") or die("数据库服务器连接错误".mysql_error());
mysql_select_db("aaa",$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 aaa 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 aaa 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 aaa 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>
数据库建立aaa 表名aaa
表结构:
id 表ID(唯一)
title 各类标题
flid 类别的ID (大类为1 中类为2 小类为3)
pid 上类的ID(大类就跟大类,提交中类的时候这地方写大类的ID,提交小类的时候写中类的ID)
$link=mysql_connect("localhost","root","123") or die("数据库服务器连接错误".mysql_error());
mysql_select_db("aaa",$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 aaa 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 aaa 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 aaa 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>
数据库建立aaa 表名aaa
表结构:
id 表ID(唯一)
title 各类标题
flid 类别的ID (大类为1 中类为2 小类为3)
pid 上类的ID(大类就跟大类,提交中类的时候这地方写大类的ID,提交小类的时候写中类的ID)
参考资料: http://hi.baidu.com/89243742/blog/item/ae1b4f23cf46795b9922ed1f.html
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询