Re: [問題] 兩層連動選單

看板PHP作者 (wayne)時間15年前 (2009/07/16 01:23), 編輯推噓1(100)
留言1則, 1人參與, 最新討論串3/3 (看更多)
※ 引述《goldenbee (bee)》之銘言: : ※ [本文轉錄自 Web_Design 看板] : 作者: goldenbee (bee) 看板: Web_Design : 標題: [問題] 兩層連動選單 : 時間: Tue Jul 14 15:29:39 2009 : 希望目的是第一個選單選了縣市後,第二個選單會出現所在縣市的鄉鎮, : 目前的做法是第一個選單選擇後,將選單資料送出到後端去查詢資料庫, : 再將第二個選單的內容送到瀏覽者的電腦中。但現在遇到的問題是第二個 : 選單的資料庫查詢語法WHRER那邊不知道要怎麼寫@@ : ex.以第一個選單為縣市(假設欄位名稱是city),第二個選單為鄉鎮為例... : 第二個選單的資料庫語法為: : SELECT 鄉鎮 : FROM 鄉鎮資料表 : WHERE 縣市=(選了第一個選單後傳來的值)這邊要怎麼寫呢? : 謝謝囉 member.php <--假設這個是會員的表單 <script> function ch_city(x,y,z) { document.getElementsByName("form1")[0].target='iframe1'; document.getElementsByName("form1")[0].action='area.php'; document.getElementsByName("form1")[0].city.value=document.getElementsByName("form1")[0].city1.value; document.getElementsByName("form1")[0].area.value=document.getElementsByName("form1")[0].area1.value; document.getElementsByName("form1")[0].post1.value=''; document.getElementsByName("form1")[0].act.value=y; document.getElementsByName("form1")[0].col_name.value=z; document.getElementsByName("form1")[0].submit(); } </script> <body> <form name="form1" method="post" onsubmit="return chk()" > <input type='hidden' name='city' /> <input type='hidden' name='area' /> <input type='hidden' name='col_name' /> <input type='hidden' name='act' /> <? $sql="select * from country group by city order by postid"; $rs=db_q($sql); echo" <select name='city1' id='city1'onchange=ch_city(this.value,'city','area1')> <option>請選擇縣市</option> "; while($row=get_data($rs)) { $name=$row[city]; $value=$row[city]; echo"<option value='".$value."'"; if($city1==$value) print"selected"; print ">$name</option> } echo"</select> $sql="select * from country where city='".$city1."' order by postid"; $rs=db_q($sql); echo" <select name='area1' id='area1' onchange=ch_city(this.value,'area','post1')> <option>鄉鎮區</option> "; while($row=get_data($rs)) { $name=$row[postid]; $value=$row[postid]; echo"<option value='".$value."'"; if($area1==$value) print"selected"; print ">$name</option>"; } echo"</select> echo"<input type='text' name='post1' size='5' value='$post1' readonly>" } </form> <iframe name="iframe1" width="0" height="0" id="iframe1"></iframe> </body> area.php <---這是處理的程式 <? echo"<script>"; if($act=="city" ) { echo" function ch_city() { "; $sel_q="select * from country where city='$city' order by postid"; $sel_r=db_q($sel_q); $num_r=mysql_num_rows($sel_r); echo"area_num=$num_r;\n"; echo"parent.form1.".$col_name.".options.length=area_num+1;\n"; echo"parent.form1.".$col_name.".options[0].value='';\n"; echo"parent.form1.".$col_name.".options[0].text='鄉鎮區';\n"; echo"parent.form1.".$col_name.".options[0].selected=true;\n"; $i=1; while($row_sel=mysql_fetch_array($sel_r)) { $var_name=$row_sel[area]; $var_value=$row_sel[postid]; echo"parent.form1.".$col_name.".options[".$i."].value='".$var_name."';\n"; echo"parent.form1.".$col_name.".options[".$i."].text='".$var_name."';\n"; $i++; } echo"} ch_city(); "; } if($act=="area") { echo" function get_postid() { "; $sel_q="select * from country where city='$city' and area='$area'"; $sel_r=db_q($sel_q); $num_r=mysql_num_rows($sel_r); if($row_sel=mysql_fetch_array($sel_r) and $num_r) { $postid=$row_sel[postid]; $part=$row_sel[part]; echo"parent.form1.".$col_name.".value='".$postid."';"; } else { echo"parent.form1.".$col_name.".value='';"; } echo"} get_postid(); "; } 以上....會把target指定到iframe是有原因的,這樣整個網頁就不會在loading一遍 像是偷偷在背後執行,要不然一般都會像網頁更新一樣會在loading一次! 這樣有了解嗎?...有問題可以再請教我喔! -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 123.192.159.178

07/17 11:51, , 1F
感謝,來研究看看
07/17 11:51, 1F
文章代碼(AID): #1ANX2Ack (PHP)
文章代碼(AID): #1ANX2Ack (PHP)