PHP+mysql實(shí)現(xiàn)的三級(jí)聯(lián)動(dòng)菜單功能示例
本文實(shí)例講述了PHP+mysql實(shí)現(xiàn)的三級(jí)聯(lián)動(dòng)菜單功能。分享給大家供大家參考,具體如下:
數(shù)據(jù)庫(kù)mysql
-- 數(shù)據(jù)庫(kù): `student` -- -- -------------------------------------------------------- -- -- 表的結(jié)構(gòu) `student` -- CREATE TABLE `student` ( `id` int(50) NOT NULL auto_increment, `name` varchar(50) collate utf8_unicode_ci NOT NULL, `dept` varchar(50) collate utf8_unicode_ci NOT NULL, `class` varchar(50) collate utf8_unicode_ci NOT NULL, `sex` varchar(50) collate utf8_unicode_ci NOT NULL, `dept_id` int(50) NOT NULL, `class_id` int(50) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=5 ; -- -- 導(dǎo)出表中的數(shù)據(jù) `student` -- INSERT INTO `student` (`id`, `name`, `dept`, `class`, `sex`, `dept_id`, `class_id`) VALUES (1, '計(jì)算機(jī)名字', '計(jì)算機(jī)工程系', '計(jì)061', '男', 1, 11), (2, '教計(jì)名字', '計(jì)算機(jī)工程系', '教技061', '男', 1, 14), (3, '管理名字', '管理系', '管理061', '女', 2, 21), (4, '機(jī)械名字', '機(jī)械工程系', '自動(dòng)化061', '男', 3, 31);
index.php代碼:
<?php
$link=mysql_connect("localhost","root","") or die("數(shù)據(jù)庫(kù)服務(wù)器連接錯(cuò)誤".mysql_error());
mysql_select_db("student",$link) or die("數(shù)據(jù)庫(kù)訪問(wèn)錯(cuò)誤".mysql_error());
mysql_query("set character set gb2312");
mysql_query("set names gb2312");
?>
<html>
<head>
<title>下拉框連動(dòng)</title>
</head>
<body>
<script language="JavaScript">
//二級(jí)菜單數(shù)組
var subcat = new Array();
<?php
$i=0;
$sql="select * from student";
$query=mysql_query($sql,$link);
while($arr=mysql_fetch_array($query))
{
echo "subcat[".$i++."] = new Array('".$arr["dept_id"]."','".$arr["class"]."','".$arr["class_id"]."');\n";
}
?>
//三級(jí)菜單數(shù)組
var subcat2 = new Array();
<?php
$i=0;
$sql="select * from student";
$query=mysql_query($sql,$link);
while($arr=mysql_fetch_array($query))
{
echo "subcat2[".$i++."] = new Array('".$arr["class_id"]."','".$arr["class"]."','".$arr["name"]."');\n";
}
?>
function changeselect1(locationid)
{
document.form1.s2.length = 0;
document.form1.s2.options[0] = new Option('22請(qǐng)選擇22','');
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('33請(qǐng)選擇33','');
for (i=0; i<subcat2.length; i++)
{
if (subcat2[i][0] == locationid)
{
document.form1.s3.options[document.form1.s3.length] = new Option(subcat2[i][2], subcat2[i][0]);
}
}
}
</script>三級(jí)聯(lián)動(dòng)演示:<BR>
<form name="form1">
<select name="s1" onChange="changeselect1(this.value)">
<option>--請(qǐng)選擇--</option>
<option value=1>計(jì)算機(jī)工程系</option>
<option value=2>管理系</option>
<option value=3>機(jī)械工程系</option>
</select>
<select name="s2" onChange="changeselect2(this.value)">
<option>--請(qǐng)選擇--</option>
</select>
<select name="s3">
<option>--請(qǐng)選擇--</option>
</select>
</form>
</body>
</html>
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php+mysql數(shù)據(jù)庫(kù)操作入門教程》、《php+mysqli數(shù)據(jù)庫(kù)程序設(shè)計(jì)技巧總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》及《php常見數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
- PHP+Mysql+Ajax+JS實(shí)現(xiàn)省市區(qū)三級(jí)聯(lián)動(dòng)
- jQuery結(jié)合PHP+MySQL實(shí)現(xiàn)二級(jí)聯(lián)動(dòng)下拉列表[實(shí)例]
- php+mysql實(shí)現(xiàn)的二級(jí)聯(lián)動(dòng)菜單效果詳解
- jQuery+PHP+MySQL二級(jí)聯(lián)動(dòng)下拉菜單實(shí)例講解
- 落伍首發(fā) php+mysql 采用ajax技術(shù)的 省 市 地 3級(jí)聯(lián)動(dòng)無(wú)刷新菜單 源碼
- 使用PHP+MySql+Ajax+jQuery實(shí)現(xiàn)省市區(qū)三級(jí)聯(lián)動(dòng)功能示例
- ThinkPHP使用心得分享-ThinkPHP + Ajax 實(shí)現(xiàn)2級(jí)聯(lián)動(dòng)下拉菜單
- php 三級(jí)聯(lián)動(dòng)菜單
- PHP+JS三級(jí)菜單聯(lián)動(dòng)菜單實(shí)現(xiàn)方法
- thinkPHP實(shí)現(xiàn)的聯(lián)動(dòng)菜單功能詳解
- PHP+ajax實(shí)現(xiàn)二級(jí)聯(lián)動(dòng)菜單功能示例
相關(guān)文章
PHP Class&Object -- 解析PHP實(shí)現(xiàn)二叉樹
本篇文章是對(duì)PHP中二叉樹的實(shí)現(xiàn)代碼進(jìn)行詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
淺析虛擬主機(jī)服務(wù)器php fsockopen函數(shù)被禁用的解決辦法
以下是對(duì)虛擬主機(jī)服務(wù)器php fsockopen函數(shù)被禁用的解決方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過(guò)來(lái)參考下2013-08-08
Zend Studio去除編輯器的語(yǔ)法警告設(shè)置方法
Zend Studio是PHP開發(fā)者的首選開發(fā)工具,其地位相當(dāng)于微軟開發(fā)工具中的Visual Studio。Zend Studio的編輯器可以幫我們指出語(yǔ)法錯(cuò)誤和警告,但是太多的警告有時(shí)讓我們的代碼看起來(lái)很亂,很不舒服2012-10-10
php使用curl實(shí)現(xiàn)簡(jiǎn)單模擬提交表單功能
這篇文章主要為大家詳細(xì)介紹了php使用curl實(shí)現(xiàn)簡(jiǎn)單模擬提交表單功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05
PHP中SESSION使用中的一點(diǎn)經(jīng)驗(yàn)總結(jié)
最近在網(wǎng)上的交流中發(fā)現(xiàn)大家對(duì)SESSION的使用上有很多誤區(qū),本質(zhì)上就是不了解SESSION的工作原理2012-03-03
比較discuz和ecshop的截取字符串函數(shù)php版
網(wǎng)上看到一篇文章 discuz和ecshop截取字符串的兩個(gè)函數(shù),比較了一下兩個(gè)版本的函數(shù),都各有局限,只能在特定的前提下使用,但是學(xué)習(xí)一下有利于拓寬思路,了解PHP的擴(kuò)展功能2012-09-09
PHP入門教程之正則表達(dá)式基本用法實(shí)例詳解(正則匹配,搜索,分割等)
這篇文章主要介紹了PHP入門教程之正則表達(dá)式基本用法,結(jié)合實(shí)例形式分析了正則表達(dá)式的結(jié)構(gòu)、原理及正則匹配、搜索、分割、元子符、修飾符等相關(guān)概念與操作技巧,需要的朋友可以參考下2016-09-09

