PHP二级联动下拉菜单,带数据库表结构
(2010-03-17 14:24:31)
标签:
杂谈
PHP代码
var onecount;
onecount=0;
subcat = new Array();
<?
mysql_connect("localhost","root","");
mysql_select_db( "test" );
$sql = "select * from subject";
$result = mysql_query( $sql );
$count = 0;
while($res = mysql_fetch_row($result)){
?>
subcat[<?=$count?>] = new
Array("<?=$res[0]?>","<?=$res[1]?>","<?=$res[2]?>");
<?
$count++;
}
echo "onecount=$count;";
?>
//联动函数
function changelocation(locationid)
{
document.myform.ctype.length = 0;
var locationid=locationid;
var i;
for (i=0;i < onecount; i++)
{
if (subcat[i][2] == locationid)
{
//var newOption1=new Option(subcat[i][1], subcat[i][0]);
//document.all.ctype.add(newOption1);
document.myform.ctype.options[document.myform.ctype.length] = new
Option(subcat[i][1], subcat[i][0]);
}
}
}
action="ru_query.php">
onChange="changelocation(document.myform.type.options[document.myform.type.selectedIndex].value)"
size="1">
value="">请指定主分类
$sql = "select * from depart";
$result = mysql_query( $sql );
while($res = mysql_fetch_row($result)){
?>
echo $res[1];
?>
} ?>
value="">请指定小分类
value="搜索">
数据库是
表depart
departID departName
表subject
subjectID subjectName departID
1.通过精选取得数据库的字段并且存入数组
2.定义自定义函数,遍历数组并且使用echo命令把$参数转化成js可以使用的变量
3.在js中使用<?自定义函数 ?>参数进行传递
4.通过js的new Option重新组织菜单选项
分享:
喜欢
0
赠金笔
加载中,请稍候......
评论加载中,请稍候...
发评论
登录名: 密码: 找回密码 注册记住登录状态
昵 称:
评论并转载此博文
发评论
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。