1. 程式人生 > >級聯菜單

級聯菜單

nth while ESS != ref pan pid left HR

<?php 
    header("content:text/html;charset=utf8");
    $con=@mysql_connect("localhost","root","");
    mysql_select_db("hnthdl",$con);
    mysql_query("set name utf8");
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<script>
    function selectcity(x){
        if(x!="請選擇"){
            window.location.href=‘test.php?pid=‘+x;
        }
    }
    function selecttown(x){
        if(x!="請選擇"){
            window.location.href=‘test.php?cid=‘+x+"&pid="+form1.hnthdl_area.value;
        }
    }
</script>
<body>
    <?php 
        session_start();
        if($_GET[pid]!=""){ $_SESSION["pid"]=$_GET[pid]; }
        if($_GET[cid]!=""){ $_SESSION["cid"]=$_GET[cid]; }
     ?>
<form  name="form1">
  <table width="300" border="0"   cellpadding="0"   cellspacing="0">   
      <tr   align="center">
        <td   height="30" colspan="2"   nowrap><span class="STYLE2">級聯菜單的應用</span></td>
      </tr>
      <tr   align="center">
        <td width="120"   height="23"   nowrap><span class="STYLE1">省級名稱:   </span></td>   
         <td width="180" align="left"   nowrap><select   name="hnthdl_area" onChange="selectcity(this.value);" > 
           <option  value="請選擇"> 請選擇</option>   
         <?php 
          $query=mysql_query("select * from hnthdl_area");
          $myrow=mysql_fetch_array($query);
          if($query==true){
            do{ 
        ?> 
           <option value="<?php echo $myrow["id"];?>"
          <?php
            if($_SESSION["pid"]!="") {
            if($_SESSION["pid"]==$myrow[id]){
                 echo "selected=\"selected\""; 
            }
        }?> ><?php echo $myrow[name];?></option>   
           <?php
          }while($myrow=mysql_fetch_array($query));
           }?>
         </select></td>
        </tr>
            <tr   align="center">
             <td   height="23"   nowrap class="STYLE1">市級名稱:</td>
                <td align="left"   nowrap> 
          <?php if($_GET[pid]!="") { ?>
        <select   name="hnthdl_area1" onChange="selecttown(this.value);">
            <option  value="請選擇" selected="selected"> 請選擇</option>             
          <?php $query=mysql_query("select * from hnthdl_area1 where pid=‘".$_GET[pid]."‘");
                    if($query==true){
          while($myrow1=mysql_fetch_array($query)){                ?>
            <option   value="<?php echo $myrow1[id];?>" 
          <?php
         if($_SESSION["cid"]!=""){
              if($_SESSION["cid"]==$myrow1[id]){
               echo "selected=\"selected\"";    
          }
           }?>><?php echo $myrow1[name1];?></option>
           <?php }}?>
          </select>
          <?php }  ?>  </td>
        </tr>
             <tr   align="center">
               <td   height="23"   nowrap class="STYLE1">縣/鎮級名稱:</td>
               <td align="left"   nowrap>
        <?php if($_GET[cid]!=""){ ?>
        <select   name="hnthdl_area1">     
        <?php 
        $query=mysql_query("select * from hnthdl_area2 where cid=‘".$_GET[cid]."‘");
        if($query==true){
                     while($myrow1=mysql_fetch_array($query)){           ?>
           <option   value="<?php echo $myrow1[id];?>"><?php echo $myrow1[name2];?></option>
           <?php }}?>
        </select>
        <?php  } ?> </td>
            </tr>   
         </table>   
    </form> 
</body>
</html>

級聯菜單