php联动下拉菜单 无刷新 数据库 本人自己修改的,经测试可用
Lepaskan: 2016-06-14 00:02:31
asal
1066 orang telah melayarinya
网上没有一个可以拿来直接用的.最终老子还是动怒了.经过不屑的努力..老子终于把一个玩意给修改成功了..心脏病都快犯了.看来还要多研究JS..
源码是从csdn转来的,忘了是谁发的了~在此感谢。
html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
">
无标题文档
$host = "localhost";
$db_name = "test";
$username = "root";
$password = "";
$c($host,$username,$password) or die("不能链接到数据库!");
$db=mysql_select_db($db_name,$conn);
//处是连接数据库的代码
$query1=mysql_query("select * from news_bclass where id_2 = '0' order by id asc");
$fMenu="";
$fValue="";
while($data1=mysql_fetch_array($query1)){
$fMenu.="\"".$data1[bname]."\",";
$fValue.="\"".$data1[id]."\",";
}
$fMenu=substr($fMenu,0,(strlen($fMenu)-1));
$fMenu="[".$fMenu."]";//*****************************得到var fMenu
$fValue=substr($fValue,0,(strlen($fValue)-1));
$fValue="[".$fValue."]";//*****************************得到var fValue
//得到*****************************var sMenu
$query1=mysql_query("select * from news_bclass where id_2 = '0' order by id asc");
while($data1=mysql_fetch_array($query1)){
$province_id1=$data1[id];
$query2=mysql_query("select * from news_sclass where parentid='$province_id1' order by parentid desc");
while($data2=mysql_fetch_array($query2)){
$num=mysql_num_rows($query2);
$i++;
$sMenu.="\"".$data2[sname]."\",";
if($i==$num){
$sMenu="[".$sMenu."],[";
$i=0;
}
}
}
$sMenu.="]";
$sMenu=str_replace("\",]","\"]",$sMenu);
$sMenu=str_replace(",[]","]",$sMenu);
if((substr($sMenu,0,2))=="[\""){
$sMenu="[".$sMenu;
}else{
$sMenu=preg_replace("/\[{3,}/is","[[\\1",$sMenu);//匹配
}
//得到*****************************var sValue
$query1=mysql_query("select * from news_bclass where id_2 = '0' order by id asc");
while($data1=mysql_fetch_array($query1)){
$province_id=$data1[id];
$sql="select * from news_sclass where parentid='$province_id' order by parentid asc";
// echo $sql;
$query2=mysql_query($sql);
while($data2=mysql_fetch_array($query2)){
$nums=mysql_num_rows($query2);
$j++;
$sValue.="\"".$data2[id]."\",";
if($j==$nums){
$sValue="[".$sValue."],[";
$j=0;
}
}
}
$sValue.="]";
$sValue=str_replace("\",]","\"]",$sValue);
$sValue=str_replace(",[]","]",$sValue);
if((substr($sValue,0,2))=="[\""){
$sValue="[".$sValue;
}else{
$sValue=preg_replace("/\[{3,}/is","[[\\1",$sValue);//匹配
}
?>
<script> <br>
var fMenu = <?php echo $fMenu; ?>; <br>
var fValue = <?php echo $fValue; ?>; <br>
var sMenu = <?php echo $sMenu; ?>; <br>
var sValue = <?php echo $sValue; ?>; <br>
<br>
var oWhere = document.all.tar; <br>
var ofMenu = document.createElement("<SELECT name='bigclass'>"); <br>
var osMenu = document.createElement("<SELECT name='smallclass'>"); <br>
with(oWhere)appendChild(ofMenu),appendChild(osMenu); <br>
<br>
createMainOptions(); <br>
createSubOptions(0); <br>
<br>
ofMenu.onchange = function() {createSubOptions(this.selectedIndex);}; <br>
function createMainOptions() { <br>
for(var i=0;i<fMenu.length;i++)ofMenu.options[i] = new Option(fMenu[i],fValue[i]); <br>
} <br>
<br>
function createSubOptions(j) { <br>
with(osMenu) { <br>
length=0; <br>
for(var i=0;i<sMenu[j].length;i++)osMenu.options[i] = new Option(sMenu[j][i],sValue[j][i]); <br>
} <br>
} <br>
</script>
<script> <br>
<br>
</script>
数据库表结构:
-- phpMyAdmin SQL Dump
-- version 2.9.2
-- http://www.phpmyadmin.net
--
-- 主机: localhost
-- 生成日期: 2007 年 10 月 14 日 03:31
-- 服务器版本: 5.0.27
-- PHP 版本: 5.2.1
--
-- 数据库: `test`
--
-- --------------------------------------------------------
--
-- 表的结构 `news_bclass`
--
CREATE TABLE `news_bclass` (
`id` int(11) NOT NULL auto_increment,
`bname` varchar(10) NOT NULL default '',
`id_2` varchar(12) NOT NULL,
KEY `id` (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=6 ;
--
-- 导出表中的数据 `news_bclass`
--
INSERT INTO `news_bclass` VALUES (1, '123', '0');
INSERT INTO `news_bclass` VALUES (2, '3123', '1');
INSERT INTO `news_bclass` VALUES (3, '321231', '1');
INSERT INTO `news_bclass` VALUES (4, '4444', '0');
INSERT INTO `news_bclass` VALUES (5, '23123', '0');
-- --------------------------------------------------------
--
-- 表的结构 `news_sclass`
--
CREATE TABLE `news_sclass` (
`id` int(11) NOT NULL auto_increment,
`sname` varchar(10) NOT NULL default '',
`parentid` int(10) NOT NULL default '0',
`bname` varchar(10) NOT NULL default '',
UNIQUE KEY `id` (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=8 ;
--
-- 导出表中的数据 `news_sclass`
--
INSERT INTO `news_sclass` VALUES (1, '123', 0, '123');
INSERT INTO `news_sclass` VALUES (2, '12312', 1, '123');
INSERT INTO `news_sclass` VALUES (3, '32123', 1, '231');
INSERT INTO `news_sclass` VALUES (4, '12312', 1, '123');
INSERT INTO `news_sclass` VALUES (5, '32123', 1, '231');
INSERT INTO `news_sclass` VALUES (6, '4444', 4, '4444');
INSERT INTO `news_sclass` VALUES (7, '4444', 4, '4444');
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel terbaru oleh pengarang
-
2024-10-22 09:46:29
-
2024-10-13 13:53:41
-
2024-10-12 12:15:51
-
2024-10-11 22:47:31
-
2024-10-11 19:36:51
-
2024-10-11 15:50:41
-
2024-10-11 15:07:41
-
2024-10-11 14:21:21
-
2024-10-11 12:59:11
-
2024-10-11 12:17:31
Topik-topik yang berkaitan
Lagi>