PHP三级联动菜单【PHP+MYSQL】

来源:互联网 发布:阿里云服务器稳定吗 编辑:程序博客网 时间:2024/06/16 22:40
  1. <?php
  2.    $link=mysql_connect("localhost","root","root") or die("数据库服务器连接错误".mysql_error());
  3.    mysql_select_db("sanji",$link) or die("数据库访问错误".mysql_error());
  4.    mysql_query("set character set gb2312");
  5.      mysql_query("set names gb2312");
  6. ?>
  7. <html>
  8. <head>
  9. <title>下拉框连动</title>
  10. </head>
  11. <body>
  12. <script language="JavaScript">
  13. <!--
  14. var subcat = new Array();
  15. <?
  16. $i=0;
  17. $sql="select * from sanji where flid=2";
  18. $query=mysql_query($sql,$link);
  19. while($arr=mysql_fetch_array($query))
  20. {
  21. echo "subcat[".$i++."] = new Array('".$arr["pid"]."','".$arr["title"]."','".$arr["id"]."');/n";
  22. }
  23. ?>
  24. var subcat2 = new Array();
  25. <?
  26. $i=0;
  27. $sql="select * from sanji where flid=3";
  28. $query=mysql_query($sql,$link);
  29. while($arr=mysql_fetch_array($query))
  30. {
  31. echo "subcat2[".$i++."] = new Array('".$arr["pid"]."','".$arr["title"]."','".$arr["id"]."');/n";
  32. }
  33. ?>
  34. function changeselect1(locationid)
  35. {
  36. document.form1.s2.length = 0;
  37. document.form1.s2.options[0] = new Option('==请选择==','');
  38. for (i=0; i<subcat.length; i++)
  39. {
  40. if (subcat[i][0] == locationid)
  41. {document.form1.s2.options[document.form1.s2.length] = new Option(subcat[i][1], subcat[i][2]);}
  42. }
  43. }
  44. function changeselect2(locationid)
  45. {
  46. document.form1.s3.length = 0;
  47. document.form1.s3.options[0] = new Option('==请选择==','');
  48. for (i=0; i<subcat2.length; i++)
  49. {
  50. if (subcat2[i][0] == locationid)
  51. {document.form1.s3.options[document.form1.s3.length] = new Option(subcat2[i][1], subcat2[i][2]);}
  52. }
  53. }
  54. //-->
  55. </script>
  56. 三级联动:<BR>
  57. <form name="form1">
  58. <select name="s1" onChange="changeselect1(this.value)">
  59. <option>==请选择==</option>
  60. <?
  61. $sql="select * from sanji where flid=1";
  62. $query=mysql_query($sql,$link);
  63. while($arr=mysql_fetch_array($query))
  64. {
  65. echo "<option value=".$arr["id"].">".$arr["title"]."</option>/n";
  66. }
  67. ?>
  68. </select>
  69. <select name="s2" onChange="changeselect2(this.value)">
  70. <option>==请选择==</option>
  71. </select>
  72. <select name="s3" onChange="alert('选选择'+this.value)">
  73. <option>==请选择==</option>
  74. </select>
  75. </form>
  76. <BR>
  77. </body>
  78. </html>(表结构:

    id 表ID(唯一)

    title 各类标题

    flid 类别的ID (大类为1 中类为2 小类为3)

    pid 上类的ID(大类就跟大类,提交中类的时候这地方写大类的ID,提交小类的时候写中类的ID) )

数据库叫"sanji"


-- -- 主机: localhost
-- 生成日期: 2009 年 11 月 03 日 15:12
-- 服务器版本: 5.0.51
-- PHP 版本: 5.2.5

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";

--
-- 数据库: `sanji`
--

  1. --
  2. -- 表的结构 `sanji`
  3. --
  4. CREATE TABLE IF NOT EXISTS `sanji` (
  5.   `id` int(10) NOT NULL auto_increment,
  6.   `title` varchar(30) collate utf8_unicode_ci NOT NULL,
  7.   `flid` int(10) NOT NULL,
  8.   `pid` int(10) NOT NULL,
  9.   PRIMARY KEY  (`id`)
  10. ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=5 ;

  1. --
  2. -- 导出表中的数据 `sanji`
  3. --
  4. INSERT INTO `sanji` (`id`, `title`, `flid`, `pid`) VALUES
  5. (1, '我是1', 1, 1),
  6. (2, '我是2,归1管', 2, 1),
  7. (3, '我是3,归2管', 3, 2),
  8. (4, '我是4,也归2管', 3, 2);

 欢迎光临:http://lxs.cncn.com/66857