昨天Jace傳授密技!原本 Ajax 回傳值我是用字串硬湊出來的,不過有密技可以用比較簡單易懂的方式呈現回傳資料,那就是PHP的json_encode!它會把陣列資料轉變成 json 的格式(其實原本 jQuery 吃的格式就是 json ),所以就不用辛苦的自己湊字串啦!提供第二種方法囉~修改過的地方用紅色標記:
index.php:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="jquery.cascade.js"></script>
<script type="text/javascript" src="jquery.cascade.ext.js"></script>
<script type="text/javascript" src="jquery.templating.js"></script>
</head>
<body>
第一項 <select id="myParentSelect">
<option value="">請選擇</option>
<?php
// 資料庫設定
$host_sql = "localhost";
$username_sql = "username";
$password_sql = "password";
$link = mysql_connect($host_sql, $username_sql, $password_sql) or die("無法連結資料庫");
mysql_select_db('target', $link);
mysql_query("SET NAMES UTF8");
$query = "SELECT id, name FROM table where lv = 1";
$result = mysql_query($query, $link);
while ($row = mysql_fetch_assoc($result)) {
echo '<option value="' . $row["id"] . '">' . $row["name"] . '</option>' . "\n";
}
?>
</select>
第二項 <select id="myFirstChildSelect">
<option value="">請選擇</option>
</select>
第三項 <select id="mySecondChildSelect">
<option value="">請選擇</option>
</select>
<script type="text/javascript">
$(function () {
// 第一階層對應第二階層
$('#myFirstChildSelect').cascade('#myParentSelect', {
ajax: {
type: "post",
url: 'action.php',
data: { act: 'first', val: $('#myParentSelect').val() },
dataType: "json"
},
template: function(item) { return "<option value='" + item.Value + "'>" + item.Text + "</option>"; },
match: function(selectedValue) { return this.When == selectedValue; }
});
// 第二階層對應第三階層
$('#mySecondChildSelect').cascade('#myFirstChildSelect', {
ajax: {
type: "post",
url: 'action.php',
data: { act: 'second', val: $('#myFirstChildSelect').val() },
dataType: "json"
},
template: function(item) { return "<option value='" + item.Value + "'>" + item.Text + "</option>"; },
match: function(selectedValue) { return this.When == selectedValue; }
});
});
</script>
</body>
</html>
action.php:
<?php
// 資料庫設定
$host_sql = "localhost";
$username_sql = "username";
$password_sql = "password";
$link = mysql_connect($host_sql, $username_sql, $password_sql) or die("無法連結資料庫");
mysql_select_db('target', $link);
mysql_query("SET NAMES UTF8");
if (!empty($_GET['act'])) {
$action = $_GET['act'];
}
if (!empty($_GET['val'])) {
$parentId = $_GET['val'];
}
$list = array();
switch ($action) {
case 'first':
$query = "SELECT id, name FROM table where lv = 2 AND parentid= $parentId";
$result = mysql_query($query, $link);
while ($row = mysql_fetch_assoc($result)) {
$arr = array ('When' => $parentId, 'Value' => $row["ID"], 'Text' => $row["NAME"]);
$list[] = $arr;
}
break;
case 'second':
default :
$query = "SELECT id, name FROM table where lv = 3 AND parentid = $parentId";
$result = mysql_query($query, $link);
while ($row = mysql_fetch_assoc($result)) {
$arr = array ('When' => $parentId, 'Value' => $row["ID"], 'Text' => $row["NAME"]);
$list[] = $arr;
}
break;
}
echo json_encode($list);
哈,這樣就清楚多了呢!當然昨天用字串組的方式也可以囉,其所達到的效果是一樣的啦!提供另一種思考方向^^
- Jun 13 Fri 2008 11:02
[AJAX] jQuery的多重下拉式選單應用 PART2
close
文章標籤
全站熱搜
留言列表