function Buildkey(num) {
var ctr=1;
document.CodeForm.empname.selectedIndex=0;
document.CodeForm.empname.options[0]=new Option("");
//num是javascript的變數
<?$sql="select name from Employee where depname=$num and status='1'";?>
//但我在下php語法時,會用到剛剛那個num
<?
$result=mysql_query($sql);
while(list($empname)=mysql_fetch_row($result))
{?>
提供一個不用自己造輪子的方法 http://www.interaktonline.com/Products/Free-Products/MXTreeMenu/Overview/
他是免費的...
If you search for a quick and easy tool to implement a tree menu – search no more. MX Tree Menu is a simple to use, easy to understand Dreamweaver extension that allows you to implement both static and dynamic tree menus. Retrieve the menu from your database and implement a dynamic menu in less than a minute or simply fill in the URLs and create a static menu. Ease navigation through your site pages!
<?
$sql="select name from Employee where status='1' and depname='應該要放javascript的num'";
$result=mysql_query($sql);
while(list($empname)=mysql_fetch_row($result))
{?>
document.CodeForm.empname.options[ctr]=new Option("<?$empname?>");
ctr=ctr+1;
<? }?>
<?
$sql="select name from Employee where status='1' and depname='應該要放javascript的num'";
$result=mysql_query($sql);
while(list($empname)=mysql_fetch_row($result))
{?>
document.CodeForm.empname.options[ctr]=new Option("<?$empname?>");
ctr=ctr+1;
<? }?>
評論
又要如何在javascript中使用php的變數
因為我選單的資料是php由mysql中抓出來的
現在程式包含了php語法還有javascript~
但我試不出來怎麼使用另一種語言的變數資料
可以試著透過PHP產生類似下面這樣的網頁:
[code]
<script type="text/javascript">
var toggle_cookie="twpug_net_newbb1_G";
</script>
[code]
接著就可以在 javascript 中引用了,多試試吧
可以麻煩你幫我看一下程式嗎?
<script LANGUAGE="javascript">
function Buildkey(num) {
var ctr=1;
document.CodeForm.empname.selectedIndex=0;
document.CodeForm.empname.options[0]=new Option("");
//num是javascript的變數
<?$sql="select name from Employee where depname=$num and status='1'";?>
//但我在下php語法時,會用到剛剛那個num
<?
$result=mysql_query($sql);
while(list($empname)=mysql_fetch_row($result))
{?>
document.CodeForm.empname.options[ctr]=new Option("$empname");
//在javascript中,我會用到php的變數empname!
ctr=ctr+1;
<? }?>
document.CodeForm.empname.length=ctr;
document.CodeForm.empname.options[0].selected=true;
}
</script>
所以變成程式全在同一頁
html語法,要顯示option的程式內容都在我剛貼的那堆程式下面!
我只是要把那個範例裡用的資料,都變成動態由資料庫讀取出來
所以才需要在php裡用到javascript的變數~
在javascript用php的變數!
那我的程式架構這樣,無法解決我所面對的問題嗎?!
http://www.interaktonline.com/Products/Free-Products/MXTreeMenu/Overview/
他是免費的...
If you search for a quick and easy tool to implement a tree menu – search no more. MX Tree Menu is a simple to use, easy to understand Dreamweaver extension that allows you to implement both static and dynamic tree menus. Retrieve the menu from your database and implement a dynamic menu in less than a minute or simply fill in the URLs and create a static menu. Ease navigation through your site pages!
但我是用linux...所以...押根沒dreamweaver!
不過我試了好多種寫法~
還是不能在javascript裡直接使用php的變數!
而且只有變數的部份怪怪的~
php也看不懂javascript的變數!
畢竟二種語言真的差別挺大的... ├004┤
用你上面的寫法是不行的
試試用GET or Cookie來實做...
改成用javascript來寫!是不是就沒問題了?
那...javascript可以連結mysql嗎?!
像php一樣~用個函數就可以了~
因為我不懂javascript..這次真的是為了二階動態選單!
可以麻煩大家幫我解答嗎! ├142┤
無法讀取...
那...像一般外面網站的二階選單
譬如選縣~跑出一堆鄉~再選鄉跑出郵遞區號!
那個資料也不少...
全都是寫死的?!
一般都會寫成js檔來呼叫
要跟php牽扯上我只有想到兩種方法
1.php將所需要的資料全部撈出給javascript使用
2.在select onchange的時候傳回給servr由php重撈一次資料
方法1會讓你的sourc code顯得較大
方法2必須再連回server讀取資料
哪種好用就端看個人或系統的需求了
你可以簡單寫個小範例給我看嗎~├142┤
用php讀資料庫的資料後~將那個資料運用在javascript中!
那我可以在javascript裡用到php的資料嗎?!
├004┤
一次將所有資料由資料庫撈出
像是地址的運用
PHP由資料庫撈出縣市名稱再丟成javascript可認得模式
組合成
City = new Array("台北市", "基隆市".....);
這樣javascript就能使用了
不好意思...反應不過來!
可以講詳細一點嗎 ├142┤
php能做到的就是動態撈取資料,將City_Ary的內容跟Country_Ary的內容從資料庫撈出排好給javascript使用
除此之外我想不出還有什麼更好的解決方法了
<script language="JavaScript">
City_Ary = new Array("臺北市", "基隆市", "臺北縣", "宜蘭縣", "新竹市", "新竹縣", "桃園縣", "苗栗縣", "臺中市", "臺中縣", "彰化縣", "南投縣", "嘉義市", "嘉義縣", "雲林縣", "臺南市", "臺南縣", "高雄市", "高雄縣", "澎湖縣", "屏東縣", "臺東縣", "花蓮縣", "金門縣", "連江縣");
Country_Ary = new Array(25);
Country_Ary[0] = new Array("中正區", "大同區", "中山區", "松山區", "大安區", "萬華區", "信義區", "士林區", "北投區", "內湖區", "南港區", "文山區(木柵)", "文山區(景美)");
Country_Ary[1] = new Array("仁愛區", "信義區", "中正區", "中山區", "安樂區", "暖暖區", "七堵區");
Country_Ary[2] = new Array("萬里鄉", "金山鄉", "板橋市", "汐止鎮", "深坑鄉", "石碇鄉", "瑞芳鎮", "平溪鄉", "雙溪鄉", "貢寮鄉", "新店市", "坪林鄉", "烏來鄉", "永和市", "中和市", "土城市", "三峽鎮", "樹林市", "鶯歌鎮", "三重市", "新莊市", "泰山鄉", "林口鄉", "蘆洲市", "五股鄉", "八里鄉", "淡水鎮", "三芝鄉", "石門鄉");
function Init_City(CityInput, CountryInput)
{
CityInput.length = City_Ary.length;
for (i = 0; i < City_Ary.length; i++)
{
CityInput.options.value = City_Ary;
CityInput.options.text = City_Ary;
}
CityInput.selectedIndex = 0;
Change_Country(CityInput, CountryInput);
}
function Change_Country(CityInput, CountryInput)
{
selectedCityIndex = CityInput.selectedIndex;
CountryInput.length = Country_Ary[selectedCityIndex].length;
for (i = 0; i < Country_Ary[selectedCityIndex].length; i++)
{
CountryInput.options.value = Country_Ary[selectedCityIndex];
CountryInput.options.text = Country_Ary[selectedCityIndex];
if (CountryInput.options.text == "")
CountryInput.options.selected = true;
}
CountryInput.selectedIndex = 0;
}
</script>
<form name="form1" action="" method="post">
<select name="CITY" onchange="Change_Country(document.form1.CITY, document.form1.COUNTRY)">
</select>
<select name="COUNTRY">
</select>
<script language="JavaSCRIPT">Init_City(document.form1.CITY, document.form1.COUNTRY)</script>
</form>
資料來源改成用php抓
但javascript不認得php的變數資料!
<script LANGUAGE="javascript">
function Buildkey(num) {
var ctr=1;
document.CodeForm.empname.selectedIndex=0;
document.CodeForm.empname.options[0]=new Option("");
<?
$sql="select name from Employee where status='1' and depname='應該要放javascript的num'";
$result=mysql_query($sql);
while(list($empname)=mysql_fetch_row($result))
{?>
document.CodeForm.empname.options[ctr]=new Option("<?$empname?>");
ctr=ctr+1;
<? }?>
document.CodeForm.empname.length=ctr;
document.CodeForm.empname.options[0].selected=true;
}
</script>
<select name="depnames" onChange="button(this.options[this.options.selectedIndex].value);">
<option value=""></option>
<?
$sql="select depname from Department";
$result=mysql_query($sql);
while(list($depname)=mysql_fetch_row($result))
echo "<option value=".$depname.">".$depname."</option>";
?>
</select>部門
<select size="1" name="empname">
<option></option>
</select>
依你的寫法
是透過select呼叫javascript傳回給php讀取資料庫
流程上有很大的問題
php是server端的程式,而javascript是client端的
user只能看到php處理好組成的html語法
之前kiang大大的回答就已經很清楚了吧 ├004┤
換個說法就是
javascript不是不認得php的變數
是根本不會看到php的變數
那你的那個範例如果要把資料來源改成php從db讀取
要朝什麼方向改啊? ├128┤
建議你先不要管php
先完成整個javascript(包含所需的全部資料)
等javascript run起來沒問題了
再反向思考php如何提供資料給javascript
很多網站的註冊住址選單
他們都是用sam0228站長提供的這個方法嗎?
還是有其他作法
@,是版工,站長是Kiang啦,大家別認錯。
我提供的只是方法中的一種而已,畢竟程式寫法很多。
只要結果正確,run起來速度沒問題,我覺得不需要太care"寫法"。