欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

代码示例展现PHP二级联动菜单的实现

程序员文章站 2022-06-07 13:46:38
...
对于初学者来说,

PHP二级联动菜单代码示例:

  1. script language = "JavaScript">
  2. var onecount;
  3. onecount=0;
  4. subcat = new Array();
  5. ?php
  6. mysql_connect("localhost","charles",
    "charles");
  7. mysql_select_db( "_crm" );
  8. $sql = "select * from site";
  9. $result = mysql_query( $sql );
  10. $count = 0;
  11. while($res = mysql_fetch_row($result)){
  12. ?>
  13. subcat[ ?php echo $count ?>] = new Array
    ("
    ?php echo $res[0]?>"," ?php echo
    $res[2]
    ?>"," ?php echo $res[1]?>");
  14. ?php
  15. $count++;
  16. }
  17. echo "onecount=$count;";
  18. ?>
  19. //联动函数
  20. function changelocation(locationid)
  21. {
  22. document.myform.ctype.length = 0;
  23. var locationidlocationid=locationid;
  24. var i;
  25. for (i=0;i onecount; i++)
  26. {
  27. if (subcat[i][2] == locationid)
  28. {
  29. //var newnewOption1=new Option(subcat[i]
    [1], subcat[i][0]);
  30. //document.all.ctype.add(newOption1);
  31. document.myform.ctype.options[document.
    myform.ctype.length] = new Option(subcat
    [i][1], subcat[i][0]);
  32. }
  33. }
  34. }
  35. /script>
  36. form method="post" name="myform"
    action="ru_query.php">
  37. select name="type" onChange="changelocation(
    document.myform.type.options[document.myfo
    rm.type.selectedIndex].value)"
    size="1">
  38. option selected value="">main /option>
  39. ?php
  40. $sql = "select * from entreprise";
  41. $result = mysql_query( $sql );
  42. while($res = mysql_fetch_row($result)){
  43. ?>
  44. option value="">
  45. ?php echo $res[1]; ?> /option>
  46. ?php } ?>
  47. /select>
  48. select name="ctype">
  49. option selected value="">sub /option>
  50. /select>
  51. input type="submit" name="Submit" value="search">
  52. /form>

以上代码就是PHP二级联动菜单的具体实现方法。