主要是想做的向下面這個網站的動態選單
http://www.ecarlink.c...nventory(all years, all makes, all models, go的那個)
主要是他前兩個year 與 makes是可以雙向
第三個models在選擇makes後 還能依據第一個年分篩選
我的資料庫如下
car#, yr, mk_nm, model
13345, 2000, toyota, camry
45678, 1999, toyota, corolla
78946, 2005, honda, civic
類似這樣
目前我只能做出偽三階選單 >"<
因為最後一個models沒辦法抓到第一個years的值
只能依據Makes的值來改變
另外 也沒辦法依據makes改變year的內容
我是參照這位大哥的文章修改的
http://bbs.mychat.to/reads.php?tid=744737但每次修改後 先不說我寫的對不對
網頁在幾次重新整理後 還是無法正常顯示最新的結果
都要關掉網頁重開或等好一段時間才行
連if else if else也都只能抓到第一個if
後面的都不跑了
只好弄成兩個if 才正常點...囧
以下是我的語法 (head前includes java部分就省略)
主頁面
<select name="yr[]" id="yr">
<option value="0">Select Year</option>
<?php
$data = "SELECT yr FROM InventoryList"; $result = mysql_query($data) or die();
while ($row = mysql_fetch_array($result)) {echo '<option value="'.$row["yr"].'">'.$row["yr"].'</option>'; }
?>
</select>
<select name="make[]" id="mk_nm">
<option value="">All Makes</option>
<?php
$data = "SELECT mk_nm FROM InventoryList"; $result = mysql_query($data) or die();
while ($row = mysql_fetch_array($result)) {echo '<option value="'.$row["mk_nm"].'">'.$row["mk_nm"].'</option>'; }
?>
</select>
<script type="text/javascript">
jQuery(document).ready(function(){ $('#mk_nm').cascade('#yr', { ajax: {type: "GET", url: 'action3.php', data: { act: 'first', val: $('#yr').val() } },
template: function(item) { return "<option value='" + item.Value + "'>" + item.Text + "</option>"; },
match: function(selectedValue) { return this.When == selectedValue; } });});
</script>
<select name="model[]" id="model">
<option value="">Select Makes</option>
</select>
<script type="text/javascript">
jQuery(document).ready(function(){ $('#model').cascade('#mk_nm', { ajax: {type: "GET", url: 'action3.php', data: { act: 'second', val: $('#mk_nm').val() } },
template: function(item) { return "<option value='" + item.Value + "'>" + item.Text + "</option>"; },
match: function(selectedValue) { return this.When == selectedValue; } });
});
</script>
-------------------------------------------
action頁面
if (isset($_GET['act'])) { $action = $_GET['act'];}
if (isset($_GET['val'])) { $parentId = $_GET['val']; }
switch ($action) {
case 'first':
$list = '[{\'When\':\'' . $parentId . '\',\'Value\':\'\',\'Text\':\'Select Make\'},';
if ($parentId == 0) { $query = "SELECT mk_nm FROM InventoryList"; }
if ($parentId != 0) { $query = "SELECT mk_nm FROM InventoryList WHERE yr = '".$parentId."' ORDER BY mk_nm ASC"; }
$result = mysql_query($query) or die();
while ($row = mysql_fetch_array($result)) { $list .= '{\'When\':\'' . $parentId . '\',\'Value\':\'' . $row["mk_nm"] . '\',\'Text\':\'' . $row["mk_nm"] . '\'},';}
break;
case 'second':
$list = '[{\'When\':\'' . $parentId . '\',\'Value\':\'\',\'Text\':\'Select Model\'},';
$query = "SELECT model FROM InventoryList WHERE mk_nm = '".$parentId."' ORDER BY model ASC";
$result = mysql_query($query) or die();
while ($row = mysql_fetch_array($result)) { $list .= '{\'When\':\'' . $parentId . '\',\'Value\':\'' . $row["model"] . '\',\'Text\':\'' . $row["model"] . '\'},';}
break;
}
$list .= ']';
echo $list;
煩請請各位高手指點建議
感激不盡>"<