<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" 
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>台扣啵的研究日誌-AJAX</title>
<link>http://blog.roodo.com/taikobo0/archives/cat_488987.html</link>
<description></description>
<language>zh-tw</language>
<generator>Roodo Blog System</generator>
<copyright>All Rights Reserved</copyright>
<atom:link href="http://blog.roodo.com/taikobo0/archives/cat_488987.xml" rel="self" type="application/rss+xml" />
<item>
	<title>[AJAX] jQuery的多重下拉式選單應用：Select box manipulation</title>
	<description><![CDATA[
			從本網誌回應數最高的文章得知，大家似乎對多重下拉式選單的功能情有獨鍾啊！Ajax 能夠在不換頁的情況下，達到資料庫連結，是許多人夢寐以求的功能；而 jQuery 易上手與輕鬆操作的特性，讓我們能更簡單的運用 Ajax 達成目的。然而隨著時間的推移，過去介紹的 cascade 已經有很長一段時間沒有更新，而且在使用上其實存在不少綁手綁腳的地方；距離 jQuery 的宗旨「Write Less, Do More」似乎是還差那麼一小段距離...經由公司的 jQuery 教育訓練，Jace 介紹了一個超讚的 jQuery Plugin：Select box manipulation。透過這個外掛的幫助，可以更輕鬆的實現多重下拉式選單的功能唷，甚至連 cascade 不容易做到的「預設值」也完全沒問題，所需要撰寫的程式碼也少於 cascade，整個就是夢幻的 Plugin！這麼神奇的外掛要怎麼用呢？以下簡單的範例，給有需要的人參考吧：範例是三階層的關聯式多重下拉式選單，分為index.php（呈現頁）、action.php（Ajax 後端資料處理頁）、index.js（JavaScript 處理）、以及 selectboxes
		]]>
	</description>
	<content:encoded><![CDATA[
			從本網誌<a href="http://blog.roodo.com/taikobo0/archives/6166625.html" target="_blank">回應數最高的文章</a>得知，大家似乎對多重下拉式選單的功能情有獨鍾啊！Ajax 能夠在不換頁的情況下，達到資料庫連結，是許多人夢寐以求的功能；而 jQuery 易上手與輕鬆操作的特性，讓我們能更簡單的運用 Ajax 達成目的。然而隨著時間的推移，過去介紹的 <a href="http://plugins.jquery.com/project/cascade" target="_blank">cascade</a> 已經有很長一段時間沒有更新，而且在使用上其實存在不少綁手綁腳的地方；距離 jQuery 的宗旨「Write Less, Do More」似乎是還差那麼一小段距離...<br /><br />經由公司的 <a href="http://blog.wabow.com/archives/289" target="_blank">jQuery 教育訓練</a>，Jace 介紹了一個超讚的 jQuery Plugin：<a href="http://www.texotela.co.uk/code/jquery/select/" target="_blank">Select box manipulation</a>。透過這個外掛的幫助，可以更輕鬆的實現多重下拉式選單的功能唷，甚至連 cascade 不容易做到的「預設值」也完全沒問題，所需要撰寫的程式碼也少於 cascade，整個就是夢幻的 Plugin！這麼神奇的外掛要怎麼用呢？以下簡單的範例，給有需要的人參考吧：範例是三階層的關聯式多重下拉式選單，分為index.php（呈現頁）、action.php（Ajax 後端資料處理頁）、index.js（JavaScript 處理）、以及 selectboxes
		<a class="acontinues" href="http://blog.roodo.com/taikobo0/archives/8671037.html">(繼續閱讀...)</a>
		]]>
	</content:encoded>
	<link>http://blog.roodo.com/taikobo0/archives/8671037.html</link>
	<guid>http://blog.roodo.com/taikobo0/archives/8671037.html</guid>
	<category>AJAX</category>
	<pubDate>Thu, 09 Apr 2009 12:05:02 +0800</pubDate>
</item>
<item>
	<title>[AJAX] JavaScript 的 PHP 函數寫法：nl2br()、htmlspecialchars()</title>
	<description><![CDATA[
			PHP 真的是一種非常方便的語言，其支援的龐大函式庫，可以讓人很輕鬆自在的使用許多實用的函數。其中常用的 nl2br() 與 htmlspecialchars() 在 HTML 顯示的時候很常使用，但是 JavaScript 就沒有提供類似方便使用的函數了；因為某些原因要在前端使用這二個函數，稍微 google 了一下馬上發現有前輩已經寫出來囉！在此作個紀錄並分享給有需要的人：（來源：Replace newlines with BR (platform safe)、Javascript 的 htmlspecialchars function 與 htmlspecialchars_decode function，感謝網路上的前輩們^^）// javascript 版本的 nl2brvar nl2br = function (string) {&nbsp;&nbsp;&nbsp; string = escape(string);&nbsp;&nbsp;&nbsp; if (string.indexOf('%0D%0A') &gt; -1) {&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; var re_nlchar = /%0D%0A/g ;&nbsp;&nbsp;&nbsp; } else if (string.indexOf('%0A') &gt; -1) {&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; var re_nlchar = /%0A/g ;&nbsp;&nbsp;&nbsp; } else if (string.indexOf('%0D') &gt; -1) {&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; var re_nlchar = /%0D/g ;&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp; return unescape(string.replace(re_nlchar, '&lt;br /&gt;'));}// javascript 版本的 htmlspecialcharsvar htmlspecialchars = function (string, quote_style) {&nbsp;&nbsp; string = string.toString();&nbsp;&nbsp; string = string.replace(/&amp;/g, '&amp;amp;');&nbsp;&nbsp; string = string.replace(/&lt;/g, '&amp;lt;');&nbsp;&nbsp; string = string.replace(/&gt;/g, '&amp;gt;');&nbsp;&nbsp; if (quote_style == 'ENT_QUOTES') {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string = string.replace(/&quot;/g, '&amp;quot;');&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string = string.replace(/\'/g, '&amp;#039;');&nbsp;&nbsp; } else if (quote_style != 'ENT_NOQUOTES') {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string = string.replace(/&quot;/g, '&amp;quot;');&nbsp;&nbsp; }&nbsp;&nbsp; return string;}
		]]>
	</description>
	<content:encoded><![CDATA[
			PHP 真的是一種非常方便的語言，其支援的龐大函式庫，可以讓人很輕鬆自在的使用許多實用的函數。其中常用的 <a href="http://tw2.php.net/nl2br" target="_blank">nl2br()</a> 與 <a href="http://tw2.php.net/htmlspecialchars" target="_blank">htmlspecialchars()</a> 在 HTML 顯示的時候很常使用，但是 JavaScript 就沒有提供類似方便使用的函數了；因為某些原因要在前端使用這二個函數，稍微 google 了一下馬上發現有前輩已經寫出來囉！在此作個紀錄並分享給有需要的人：（來源：<a href="http://snipplr.com/view/634/replace-newlines-with-br-platform-safe/" target="_blank">Replace newlines with BR (platform safe)</a>、<a href="http://osric-life.blogspot.com/2008/07/javascript-htmlspecialchars-function.html" target="_blank">Javascript 的 htmlspecialchars function 與 htmlspecialchars_decode function</a>，感謝網路上的前輩們^^）<br /><pre class="code">// javascript 版本的 nl2br<br />var nl2br = function (string) {<br />&nbsp;&nbsp;&nbsp; string = escape(string);<br />&nbsp;&nbsp;&nbsp; if (string.indexOf('%0D%0A') &gt; -1) {<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; var re_nlchar = /%0D%0A/g ;<br />&nbsp;&nbsp;&nbsp; } else if (string.indexOf('%0A') &gt; -1) {<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; var re_nlchar = /%0A/g ;<br />&nbsp;&nbsp;&nbsp; } else if (string.indexOf('%0D') &gt; -1) {<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; var re_nlchar = /%0D/g ;<br />&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp; return unescape(string.replace(re_nlchar, '&lt;br /&gt;'));<br />}<br /><br /></pre><pre class="code">// javascript 版本的 htmlspecialchars<br />var htmlspecialchars = function (string, quote_style) {<br />&nbsp;&nbsp; string = string.toString();<br /><br />&nbsp;&nbsp; string = string.replace(/&amp;/g, '&amp;amp;');<br />&nbsp;&nbsp; string = string.replace(/&lt;/g, '&amp;lt;');<br />&nbsp;&nbsp; string = string.replace(/&gt;/g, '&amp;gt;');<br /><br />&nbsp;&nbsp; if (quote_style == 'ENT_QUOTES') {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string = string.replace(/&quot;/g, '&amp;quot;');<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string = string.replace(/\'/g, '&amp;#039;');<br />&nbsp;&nbsp; } else if (quote_style != 'ENT_NOQUOTES') {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string = string.replace(/&quot;/g, '&amp;quot;');<br />&nbsp;&nbsp; }<br /><br />&nbsp;&nbsp; return string;<br />}</pre>
		
		]]>
	</content:encoded>
	<link>http://blog.roodo.com/taikobo0/archives/8414875.html</link>
	<guid>http://blog.roodo.com/taikobo0/archives/8414875.html</guid>
	<category>AJAX</category>
	<pubDate>Tue, 03 Mar 2009 15:14:17 +0800</pubDate>
</item>
<item>
	<title>[AJAX] jQuery的多重下拉式選單應用，當有預設值的時候</title>
	<description><![CDATA[
			在多重下拉式選單的應用中，常常也會遇到已經有預設值的時候，這時候該怎麼辦呢？其實前陣子我也遇過這個問題，當初還花了一點時間尋找 jQuery的cascade 是不是有提供參數讓我輕鬆預設；不過拜完 Google 大神之後似乎是沒有什麼線索；所以最後我採用了一個很笨的方法，在頁面讀取完畢時如果有預設值的話，就直接丟一個 Ajax 取得第二層的選項。以下是實作的程式碼：（目前我只實作到二階層）index.php：&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;&lt;head&gt;&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot; /&gt;&lt;script type=&quot;text/javascript&quot; src=&quot;jquery.js&quot;&gt;&lt;/script&gt;&lt;script type=&quot;text/javascript&quot; src=&quot;jquery.cascade.js&quot;&gt;&lt;/script&gt;&lt;script type=&quot;text/javascript&quot; src=&quot;jquery.cascade.ext.js&quot;&gt;&lt;/script&gt;&lt;script type=&quot;text/javascript&quot; src=&quot;jquery.templating.js&quot;&gt;&lt;/script&gt;&lt;/head&gt;&lt;body&gt;第一項 &lt;select id=&quot;myParentSelect&quot;&gt;&lt;option value=&quot;&quot;&gt;請選擇&lt;/option&gt;&lt;?php&nbsp;&nbsp;&nbsp; // 資料庫設定&nbsp;&nbsp;&nbsp; $host_sql = &quot;localhost&quot;;&nbsp;&nbsp;&nbsp; $username_sql = &quot;username&quot;;&nbsp;&nbsp;&nbsp; $password_sql = &quot;password&quot;;&nbsp;&nbsp;&nbsp; // 預設值設定&nbsp;&nbsp;&nbsp; $defaultParentId = '3';&nbsp;&nbsp;&nbsp; $defaultChildId = '13';&nbsp;&nbsp;&nbsp; $link = mysql_connect($host_sql, $username_sql, $password_sql) or die(&quot;無法連結資料庫&quot;);&nbsp;&nbsp;&nbsp; mysql_select_db('target', $link);&nbsp;&nbsp;&nbsp; mysql_query(&quot;SET NAMES UTF8&quot;);&nbsp;&nbsp;&nbsp; $query = &quot;SELECT id, name FROM table where lv = 1&quot;;&nbsp;&nbsp;&nbsp; $result = mysql_query($query, $link);&nbsp;&nbsp;&nbsp; while ($row = mysql_fetch_assoc($result)) {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; echo '&lt;option value=&quot;' . $row[&quot;id&quot;] . '&quot;&gt;' . $row[&quot;name&quot;] . '&lt;/option&gt;' . &quot;\n&quot;;&nbsp;&nbsp;&nbsp; }?&gt;&lt;/select&gt;&lt;!-- 設定一個隱藏的欄位紀錄預設值 --&gt;&lt;input type=&quot;hidden&quot; name=&quot;defaultParentId&quot; value=&quot;&lt;?php echo $defaultParentId; ?&gt;&quot; /&gt;&lt;input type=&quot;hidden&quot; name=&quot;defaultChildId&quot; value=&quot;&lt;?php echo $defaultChildId; ?&gt;&quot; /&gt;　第二項 &lt;select id=&quot;myChildSelect&quot;&gt;&lt;option value=&quot;&quot;&gt;請選擇&lt;/option&gt;&lt;/select&gt;&lt;script type=&quot;text/javascript&quot;&gt;// 如果已有預設值，即時送出 Ajax 呼叫function getSelectedList(defaultParentId, defaultFirstChildId) {&nbsp;&nbsp;&nbsp; $.ajax({&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; type: &quot;get&quot;,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; url:&nbsp; 'action.php',&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; data: { act: 'default', val: defaultParentId, child: defaultFirstChildId },&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dataType: &quot;json&quot;,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; success: function (json) {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $('select#myChildSelect').append(json.data);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp; });};$(function () {&nbsp;&nbsp;&nbsp; // 檢查是否有預設值&nbsp;&nbsp;&nbsp; if ($('input[name=defaultParentId]').val()) {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; getSelectedList ($('input[name=defaultParentId]').val(), $('input[name=defaultChildId]').val());&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp; // 第一階層對應第二階層&nbsp;&nbsp;&nbsp; $('#myChildSelect').cascade('#myParentSelect', {&nbsp;&nbsp;&nbsp; ajax: {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; type: &quot;post&quot;,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; url:&nbsp; 'action.php',&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; data: { act: 'first', val: $('#myParentSelect').val() },&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;  dataType: &quot;json&quot;,&nbsp;&nbsp;&nbsp; },&nbsp;&nbsp;&nbsp; template: function(item) { return &quot;&lt;option value='&quot; + item.Value + &quot;'&gt;&quot; + item.Text + &quot;&lt;/option&gt;&quot;; },&nbsp;&nbsp;&nbsp; match: function(selectedValue) { return this.When == selectedValue; }&nbsp;&nbsp;&nbsp; });});&lt;/script&gt;&lt;/body&gt;&lt;/html&gt;action.php：&lt;?php// 資料庫設定$host_sql = &quot;localhost&quot;;$username_sql = &quot;username&quot;;$password_sql = &quot;password&quot;;$link = mysql_connect($host_sql, $username_sql, $password_sql) or die(&quot;無法連結資料庫&quot;);mysql_select_db('target', $link);mysql_query(&quot;SET NAMES UTF8&quot;);if (!empty($_GET['act'])) {&nbsp;&nbsp;&nbsp; $action = $_GET['act'];}if (!empty($_GET['val'])) {&nbsp;&nbsp;&nbsp; $parentId = $_GET['val'];}if (!empty($_GET['child'])) {&nbsp;&nbsp;&nbsp; $childId = $_GET['child'];}switch ($action) {&nbsp;&nbsp;&nbsp; case 'first':&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $list = array();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $query = &quot;SELECT id, name FROM table where lv = 2 AND parentid= $parentId&quot;;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $result = mysql_query($query, $link);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while ($row = mysql_fetch_assoc($result)) {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $arr = array ('When' =&gt; $parentId, 'Value' =&gt; $row[&quot;ID&quot;], 'Text' =&gt; $row[&quot;NAME&quot;]);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $list[] = $arr;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;&nbsp;&nbsp;&nbsp; case 'default':&nbsp;&nbsp;&nbsp; default :&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $list = '';&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $query = &quot;SELECT id, name FROM table where lv = 2 AND parentid= $parentId&quot;; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $result = mysql_query($query, $link); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while ($row = mysql_fetch_assoc($result)) {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $list .= '&lt;option value=&quot;' . $row[&quot;ID&quot;] . '&quot;';&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if ($childId == $row[&quot;ID&quot;]) {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $list .= ' selected=&quot;selected&quot;';&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $list .= '&gt;' . $row[&quot;NAME&quot;] . '&lt;/option&gt;';&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $list = array('data' =&gt; $list); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;}echo json_encode($list);第二階層的選項，其實是用字串硬湊出來的，所以程式碼看起來還蠻醜的，請見諒。基本上用的概念只是在頁面讀取完畢以後直接送出 Ajax 而已，是蠻笨的方法；不過也不失為解決問題的一種方法啦！如果有其他適合的解法，有希望高手們能提供一下嚕！謝謝^^
		]]>
	</description>
	<content:encoded><![CDATA[
			在多重下拉式選單的應用中，常常也會遇到已經有預設值的時候，這時候該怎麼辦呢？其實前陣子我也遇過這個問題，當初還花了一點時間尋找 <a href="http://plugins.jquery.com/project/cascade" target="_blank">jQuery的cascade</a> 是不是有提供參數讓我輕鬆預設；不過拜完 Google 大神之後似乎是沒有什麼線索；所以最後我採用了一個很笨的方法，在頁面讀取完畢時如果有預設值的話，就直接丟一個 Ajax 取得第二層的選項。以下是實作的程式碼：（目前我只實作到二階層）<br /><br />index.php：<br /><br />&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;<br />&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;<br />&lt;head&gt;<br />&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot; /&gt;<br />&lt;script type=&quot;text/javascript&quot; src=&quot;jquery.js&quot;&gt;&lt;/script&gt;<br />&lt;script type=&quot;text/javascript&quot; src=&quot;jquery.cascade.js&quot;&gt;&lt;/script&gt;<br />&lt;script type=&quot;text/javascript&quot; src=&quot;jquery.cascade.ext.js&quot;&gt;&lt;/script&gt;<br />&lt;script type=&quot;text/javascript&quot; src=&quot;jquery.templating.js&quot;&gt;&lt;/script&gt;<br />&lt;/head&gt;<br />&lt;body&gt;<br />第一項 &lt;select id=&quot;myParentSelect&quot;&gt;<br />&lt;option value=&quot;&quot;&gt;請選擇&lt;/option&gt;<br />&lt;?php<br />&nbsp;&nbsp;&nbsp; // 資料庫設定<br />&nbsp;&nbsp;&nbsp; $host_sql = &quot;localhost&quot;;<br />&nbsp;&nbsp;&nbsp; $username_sql = &quot;username&quot;;<br />&nbsp;&nbsp;&nbsp; $password_sql = &quot;password&quot;;<br /><br />&nbsp;&nbsp;&nbsp; <font color="#ff0000">// 預設值設定<br />&nbsp;&nbsp;&nbsp; $defaultParentId = '3';<br />&nbsp;&nbsp;&nbsp; $defaultChildId = '13';</font><br /><br />&nbsp;&nbsp;&nbsp; $link = mysql_connect($host_sql, $username_sql, $password_sql) or die(&quot;無法連結資料庫&quot;);<br />&nbsp;&nbsp;&nbsp; mysql_select_db('target', $link);<br />&nbsp;&nbsp;&nbsp; mysql_query(&quot;SET NAMES UTF8&quot;);<br /><br />&nbsp;&nbsp;&nbsp; $query = &quot;SELECT id, name FROM table where lv = 1&quot;;<br />&nbsp;&nbsp;&nbsp; $result = mysql_query($query, $link);<br />&nbsp;&nbsp;&nbsp; while ($row = mysql_fetch_assoc($result)) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; echo '&lt;option value=&quot;' . $row[&quot;id&quot;] . '&quot;&gt;' . $row[&quot;name&quot;] . '&lt;/option&gt;' . &quot;\n&quot;;<br />&nbsp;&nbsp;&nbsp; }<br /><br />?&gt;<br />&lt;/select&gt;<br /><br /><font color="#ff0000">&lt;!-- 設定一個隱藏的欄位紀錄預設值 --&gt;<br />&lt;input type=&quot;hidden&quot; name=&quot;defaultParentId&quot; value=&quot;&lt;?php echo $defaultParentId; ?&gt;&quot; /&gt;<br />&lt;input type=&quot;hidden&quot; name=&quot;defaultChildId&quot; value=&quot;&lt;?php echo $defaultChildId; ?&gt;&quot; /&gt;</font><br />　<br />第二項 &lt;select id=&quot;myChildSelect&quot;&gt;<br />&lt;option value=&quot;&quot;&gt;請選擇&lt;/option&gt;<br />&lt;/select&gt;<br /><br />&lt;script type=&quot;text/javascript&quot;&gt;<br /><br /><font color="#ff0000">// 如果已有預設值，即時送出 Ajax 呼叫<br />function getSelectedList(defaultParentId, defaultFirstChildId) {<br />&nbsp;&nbsp;&nbsp; $.ajax({<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; type: &quot;get&quot;,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; url:&nbsp; 'action.php',<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; data: { act: 'default', val: defaultParentId, child: defaultFirstChildId },<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dataType: &quot;json&quot;,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; success: function (json) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $('select#myChildSelect').append(json.data);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp; });<br />};</font><br /><br />$(function () {<br /><br /><font color="#ff0000">&nbsp;&nbsp;&nbsp; // 檢查是否有預設值<br />&nbsp;&nbsp;&nbsp; if ($('input[name=defaultParentId]').val()) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; getSelectedList ($('input[name=defaultParentId]').val(), $('input[name=defaultChildId]').val());<br />&nbsp;&nbsp;&nbsp; }</font><br /><br />&nbsp;&nbsp;&nbsp; // 第一階層對應第二階層<br />&nbsp;&nbsp;&nbsp; $('#myChildSelect').cascade('#myParentSelect', {<br />&nbsp;&nbsp;&nbsp; ajax: {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; type: &quot;post&quot;,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; url:&nbsp; 'action.php',<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; data: { act: 'first', val: $('#myParentSelect').val() },<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;  <font color="#000000">dataType: &quot;json&quot;,</font><br />&nbsp;&nbsp;&nbsp; },<br />&nbsp;&nbsp;&nbsp; template: function(item) { return &quot;&lt;option value='&quot; + item.Value + &quot;'&gt;&quot; + item.Text + &quot;&lt;/option&gt;&quot;; },<br />&nbsp;&nbsp;&nbsp; match: function(selectedValue) { return this.When == selectedValue; }<br />&nbsp;&nbsp;&nbsp; });<br />});<br />&lt;/script&gt;<br />&lt;/body&gt;<br />&lt;/html&gt;<br /><br />action.php：<br /><br />&lt;?php<br /><br />// 資料庫設定<br />$host_sql = &quot;localhost&quot;;<br />$username_sql = &quot;username&quot;;<br />$password_sql = &quot;password&quot;;<br /><br />$link = mysql_connect($host_sql, $username_sql, $password_sql) or die(&quot;無法連結資料庫&quot;);<br />mysql_select_db('target', $link);<br />mysql_query(&quot;SET NAMES UTF8&quot;);<br /><br />if (!empty($_GET['act'])) {<br />&nbsp;&nbsp;&nbsp; $action = $_GET['act'];<br />}<br /><br />if (!empty($_GET['val'])) {<br />&nbsp;&nbsp;&nbsp; $parentId = $_GET['val'];<br />}<br /><br /><font color="#ff0000">if (!empty($_GET['child'])) {<br />&nbsp;&nbsp;&nbsp; $childId = $_GET['child'];<br />}</font><br /><br />switch ($action) {<br />&nbsp;&nbsp;&nbsp; case 'first':<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <font color="#ff0000">$list = array();</font><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $query = &quot;SELECT id, name FROM table where lv = 2 AND parentid= $parentId&quot;;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $result = mysql_query($query, $link);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while ($row = mysql_fetch_assoc($result)) {<br /><font color="#000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </font><font color="#000000">$arr = array ('When' =&gt; $parentId, 'Value' =&gt; $row[&quot;ID&quot;], 'Text' =&gt; $row[&quot;NAME&quot;]);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $list[] = $arr;</font><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;<br />&nbsp;&nbsp;&nbsp; <font color="#ff0000">case 'default':</font><br />&nbsp;&nbsp;&nbsp; <font color="#ff0000">default :<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $list = '';<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $query = &quot;SELECT id, name FROM table where lv = 2 AND parentid= $parentId&quot;;<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $result = mysql_query($query, $link);<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while ($row = mysql_fetch_assoc($result)) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $list .= '&lt;option value=&quot;' . $row[&quot;ID&quot;] . '&quot;';<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if ($childId == $row[&quot;ID&quot;]) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $list .= ' selected=&quot;selected&quot;';<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $list .= '&gt;' . $row[&quot;NAME&quot;] . '&lt;/option&gt;';<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $list = array('data' =&gt; $list);</font><br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;<br />}<br /><br /><font color="#000000">echo json_encode($list);</font><br /><br />第二階層的選項，其實是用字串硬湊出來的，所以程式碼看起來還蠻醜的，請見諒。基本上用的概念只是在頁面讀取完畢以後直接送出 Ajax 而已，是蠻笨的方法；不過也不失為解決問題的一種方法啦！如果有其他適合的解法，有希望高手們能提供一下嚕！謝謝^^
		
		]]>
	</content:encoded>
	<link>http://blog.roodo.com/taikobo0/archives/7101187.html</link>
	<guid>http://blog.roodo.com/taikobo0/archives/7101187.html</guid>
	<category>AJAX</category>
	<pubDate>Wed, 03 Sep 2008 10:32:59 +0800</pubDate>
</item>
<item>
	<title>[AJAX] jQuery 的 lightBox</title>
	<description><![CDATA[
			一般網頁上圖片的展示，很難讓人不聯想到 Lightbox；這個 Prototype 的燈箱特效。無奈現在公司使用的 jQuery 與 Lightbox 犯沖～沒辦法讓基於 Prototype 寫成的這個外掛直接引用。雖然如此，這麼經典的覽圖介面 jQuery 怎麼會放過呢？所以，jQuery lightBox plugin 就因此誕生了！基本上官方網站已經講的非常清楚了，使用方法也跟 Lightbox 非常類似；不過更方便，也更有彈性。只要在需引用的網頁中載入：&lt;script type=&quot;text/javascript&quot; src=&quot;js/jquery.js&quot;&gt;&lt;/script&gt;&lt;script type=&quot;text/javascript&quot; src=&quot;js/jquery.lightbox-0.5.js&quot;&gt;&lt;/script&gt;&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;css/jquery.lightbox-0.5.css&quot; media=&quot;screen&quot; /&gt;之後，寫一隻小小的 JavaScript 於頁面中：&lt;script type=&quot;text/javascript&quot;&gt;$(function() {&nbsp;&nbsp;&nbsp; $('a[@rel*=lightbox]').lightBox(); });&lt;/script&gt;就可以完全相容 Lightbox 的使用環境，無痛移植。此外，jQuery lightBox plugin 提供很多可以自訂的參數，自由度提高許多！
		]]>
	</description>
	<content:encoded><![CDATA[
			一般網頁上圖片的展示，很難讓人不聯想到 <a href="http://www.huddletogether.com/projects/lightbox2/" target="_blank">Lightbox</a>；這個 <a rel="nofollow" href="http://prototype.conio.net/" title="http://prototype.conio.net/" class="external text">Prototype</a> 的燈箱特效。無奈現在公司使用的 <a href="http://jquery.com/" target="_blank">jQuery</a> 與 Lightbox 犯沖～沒辦法讓基於 Prototype 寫成的這個外掛直接引用。雖然如此，這麼經典的覽圖介面 jQuery 怎麼會放過呢？所以，<a href="http://leandrovieira.com/projects/jquery/lightbox/" target="_blank">jQuery lightBox plugin</a> 就因此誕生了！<br /><br />基本上官方網站已經講的非常清楚了，使用方法也跟 Lightbox 非常類似；不過更方便，也更有彈性。只要在需引用的網頁中載入：<br /><br />&lt;script type=&quot;text/javascript&quot; src=&quot;js/jquery.js&quot;&gt;&lt;/script&gt;<br />&lt;script type=&quot;text/javascript&quot; src=&quot;js/jquery.lightbox-0.5.js&quot;&gt;&lt;/script&gt;<br />&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;css/jquery.lightbox-0.5.css&quot; media=&quot;screen&quot; /&gt;<br /><br />之後，寫一隻小小的 JavaScript 於頁面中：<br /><br />&lt;script type=&quot;text/javascript&quot;&gt;<br />$(function() {<br />&nbsp;&nbsp;&nbsp; $('a[@rel*=lightbox]').lightBox(); <br />});<br />&lt;/script&gt;<br /><br />就可以完全相容 Lightbox 的使用環境，無痛移植。此外，jQuery lightBox plugin 提供很多可以<a href="http://leandrovieira.com/projects/jquery/lightbox/#extend" target="_blank">自訂的參數</a>，自由度提高許多！
		
		]]>
	</content:encoded>
	<link>http://blog.roodo.com/taikobo0/archives/7082729.html</link>
	<guid>http://blog.roodo.com/taikobo0/archives/7082729.html</guid>
	<category>AJAX</category>
	<pubDate>Mon, 01 Sep 2008 10:23:48 +0800</pubDate>
</item>
<item>
	<title>[AJAX] jQuery的多重下拉式選單應用 PART2</title>
	<description><![CDATA[
			昨天Jace傳授密技！原本 Ajax 回傳值我是用字串硬湊出來的，不過有密技可以用比較簡單易懂的方式呈現回傳資料，那就是PHP的json_encode！它會把陣列資料轉變成 json 的格式（其實原本 jQuery 吃的格式就是 json ），所以就不用辛苦的自己湊字串啦！提供第二種方法囉～修改過的地方用紅色標記：index.php：&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;&lt;head&gt;&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot; /&gt;&lt;script type=&quot;text/javascript&quot; src=&quot;jquery.js&quot;&gt;&lt;/script&gt;&lt;script type=&quot;text/javascript&quot; src=&quot;jquery.cascade.js&quot;&gt;&lt;/script&gt;&lt;script type=&quot;text/javascript&quot; src=&quot;jquery.cascade.ext.js&quot;&gt;&lt;/script&gt;&lt;script type=&quot;text/javascript&quot; src=&quot;jquery.templating.js&quot;&gt;&lt;/script&gt;&lt;/head&gt;&lt;body&gt;第一項 &lt;select id=&quot;myParentSelect&quot;&gt;&lt;option value=&quot;&quot;&gt;請選擇&lt;/option&gt;&lt;?php&nbsp;&nbsp;&nbsp; // 資料庫設定&nbsp;&nbsp;&nbsp; $host_sql = &quot;localhost&quot;;&nbsp;&nbsp;&nbsp; $username_sql = &quot;username&quot;;&nbsp;&nbsp;&nbsp; $password_sql = &quot;password&quot;;&nbsp;&nbsp;&nbsp; $link = mysql_connect($host_sql, $username_sql, $password_sql) or die(&quot;無法連結資料庫&quot;);&nbsp;&nbsp;&nbsp; mysql_select_db('target', $link);&nbsp;&nbsp;&nbsp; mysql_query(&quot;SET NAMES UTF8&quot;);&nbsp;&nbsp;&nbsp; $query = &quot;SELECT id, name FROM table where lv = 1&quot;;&nbsp;&nbsp;&nbsp; $result = mysql_query($query, $link);&nbsp;&nbsp;&nbsp; while ($row = mysql_fetch_assoc($result)) {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; echo '&lt;option value=&quot;' . $row[&quot;id&quot;] . '&quot;&gt;' . $row[&quot;name&quot;] . '&lt;/option&gt;' . &quot;\n&quot;;&nbsp;&nbsp;&nbsp; }?&gt;&lt;/select&gt;　第二項 &lt;select id=&quot;myFirstChildSelect&quot;&gt;&lt;option value=&quot;&quot;&gt;請選擇&lt;/option&gt;&lt;/select&gt;第三項 &lt;select id=&quot;mySecondChildSelect&quot;&gt;&lt;option value=&quot;&quot;&gt;請選擇&lt;/option&gt;&lt;/select&gt;&lt;script type=&quot;text/javascript&quot;&gt;$(function () {&nbsp;&nbsp;&nbsp; // 第一階層對應第二階層&nbsp;&nbsp;&nbsp; $('#myFirstChildSelect').cascade('#myParentSelect', {&nbsp;&nbsp;&nbsp; ajax: {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; type: &quot;post&quot;,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; url:&nbsp; 'action.php',&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; data: { act: 'first', val: $('#myParentSelect').val() },&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;  dataType: &quot;json&quot;&nbsp;&nbsp;&nbsp; },&nbsp;&nbsp;&nbsp; template: function(item) { return &quot;&lt;option value='&quot; + item.Value + &quot;'&gt;&quot; + item.Text + &quot;&lt;/option&gt;&quot;; },&nbsp;&nbsp;&nbsp; match: function(selectedValue) { return this.When == selectedValue; }&nbsp;&nbsp;&nbsp; });&nbsp;&nbsp;&nbsp; // 第二階層對應第三階層&nbsp;&nbsp;&nbsp; $('#mySecondChildSelect').cascade('#myFirstChildSelect', {&nbsp;&nbsp;&nbsp; ajax: {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; type: &quot;post&quot;,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; url:&nbsp; 'action.php',&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; data: { act: 'second', val: $('#myFirstChildSelect').val() },&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;  dataType: &quot;json&quot;&nbsp;&nbsp;&nbsp; },&nbsp;&nbsp;&nbsp; template: function(item) { return &quot;&lt;option value='&quot; + item.Value + &quot;'&gt;&quot; + item.Text + &quot;&lt;/option&gt;&quot;; },&nbsp;&nbsp;&nbsp; match: function(selectedValue) { return this.When == selectedValue; }&nbsp;&nbsp;&nbsp; });});&lt;/script&gt;&lt;/body&gt;&lt;/html&gt;action.php：&lt;?php// 資料庫設定$host_sql = &quot;localhost&quot;;$username_sql = &quot;username&quot;;$password_sql = &quot;password&quot;;$link = mysql_connect($host_sql, $username_sql, $password_sql) or die(&quot;無法連結資料庫&quot;);mysql_select_db('target', $link);mysql_query(&quot;SET NAMES UTF8&quot;);if (!empty($_GET['act'])) {&nbsp;&nbsp;&nbsp; $action = $_GET['act'];}if (!empty($_GET['val'])) {&nbsp;&nbsp;&nbsp; $parentId = $_GET['val'];}$list = array();switch ($action) {&nbsp;&nbsp;&nbsp; case 'first':&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $query = &quot;SELECT id, name FROM table where lv = 2 AND parentid= $parentId&quot;;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $result = mysql_query($query, $link);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while ($row = mysql_fetch_assoc($result)) {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $arr = array ('When' =&gt; $parentId, 'Value' =&gt; $row[&quot;ID&quot;], 'Text' =&gt; $row[&quot;NAME&quot;]);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $list[] = $arr;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;&nbsp;&nbsp;&nbsp; case 'second':&nbsp;&nbsp;&nbsp; default :&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $query = &quot;SELECT id, name FROM table where lv = 3 AND parentid = $parentId&quot;;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $result = mysql_query($query, $link);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while ($row = mysql_fetch_assoc($result)) {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $arr = array ('When' =&gt; $parentId, 'Value' =&gt; $row[&quot;ID&quot;], 'Text' =&gt; $row[&quot;NAME&quot;]); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $list[] = $arr;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;}echo json_encode($list);哈，這樣就清楚多了呢！當然昨天用字串組的方式也可以囉，其所達到的效果是一樣的啦！提供另一種思考方向^^
		]]>
	</description>
	<content:encoded><![CDATA[
			昨天Jace傳授密技！原本 Ajax 回傳值我是用字串硬湊出來的，不過有密技可以用比較簡單易懂的方式呈現回傳資料，那就是PHP的<a href="http://tw.php.net/manual/en/function.json-encode.php" target="_blank">json_encode</a>！它會把陣列資料轉變成 json 的格式（其實原本 jQuery 吃的格式就是 json ），所以就不用辛苦的自己湊字串啦！提供第二種方法囉～修改過的地方用<font color="#ff0000">紅色</font>標記：<br /><br />index.php：<br /><br />&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;<br />&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;<br />&lt;head&gt;<br />&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot; /&gt;<br />&lt;script type=&quot;text/javascript&quot; src=&quot;jquery.js&quot;&gt;&lt;/script&gt;<br />&lt;script type=&quot;text/javascript&quot; src=&quot;jquery.cascade.js&quot;&gt;&lt;/script&gt;<br />&lt;script type=&quot;text/javascript&quot; src=&quot;jquery.cascade.ext.js&quot;&gt;&lt;/script&gt;<br />&lt;script type=&quot;text/javascript&quot; src=&quot;jquery.templating.js&quot;&gt;&lt;/script&gt;<br />&lt;/head&gt;<br />&lt;body&gt;<br />第一項 &lt;select id=&quot;myParentSelect&quot;&gt;<br />&lt;option value=&quot;&quot;&gt;請選擇&lt;/option&gt;<br />&lt;?php<br />&nbsp;&nbsp;&nbsp; // 資料庫設定<br />&nbsp;&nbsp;&nbsp; $host_sql = &quot;localhost&quot;;<br />&nbsp;&nbsp;&nbsp; $username_sql = &quot;username&quot;;<br />&nbsp;&nbsp;&nbsp; $password_sql = &quot;password&quot;;<br /><br />&nbsp;&nbsp;&nbsp; $link = mysql_connect($host_sql, $username_sql, $password_sql) or die(&quot;無法連結資料庫&quot;);<br />&nbsp;&nbsp;&nbsp; mysql_select_db('target', $link);<br />&nbsp;&nbsp;&nbsp; mysql_query(&quot;SET NAMES UTF8&quot;);<br /><br />&nbsp;&nbsp;&nbsp; $query = &quot;SELECT id, name FROM table where lv = 1&quot;;<br />&nbsp;&nbsp;&nbsp; $result = mysql_query($query, $link);<br />&nbsp;&nbsp;&nbsp; while ($row = mysql_fetch_assoc($result)) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; echo '&lt;option value=&quot;' . $row[&quot;id&quot;] . '&quot;&gt;' . $row[&quot;name&quot;] . '&lt;/option&gt;' . &quot;\n&quot;;<br />&nbsp;&nbsp;&nbsp; }<br /><br />?&gt;<br />&lt;/select&gt;　<br />第二項 &lt;select id=&quot;myFirstChildSelect&quot;&gt;<br />&lt;option value=&quot;&quot;&gt;請選擇&lt;/option&gt;<br />&lt;/select&gt;<br />第三項 &lt;select id=&quot;mySecondChildSelect&quot;&gt;<br />&lt;option value=&quot;&quot;&gt;請選擇&lt;/option&gt;<br />&lt;/select&gt;<br /><br />&lt;script type=&quot;text/javascript&quot;&gt;<br />$(function () {<br /><br />&nbsp;&nbsp;&nbsp; // 第一階層對應第二階層<br />&nbsp;&nbsp;&nbsp; $('#myFirstChildSelect').cascade('#myParentSelect', {<br />&nbsp;&nbsp;&nbsp; ajax: {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; type: &quot;post&quot;,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; url:&nbsp; 'action.php',<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; data: { act: 'first', val: $('#myParentSelect').val() },<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;  <font color="#ff0000">dataType: &quot;json&quot;</font><br />&nbsp;&nbsp;&nbsp; },<br />&nbsp;&nbsp;&nbsp; template: function(item) { return &quot;&lt;option value='&quot; + item.Value + &quot;'&gt;&quot; + item.Text + &quot;&lt;/option&gt;&quot;; },<br />&nbsp;&nbsp;&nbsp; match: function(selectedValue) { return this.When == selectedValue; }<br />&nbsp;&nbsp;&nbsp; });<br /><br />&nbsp;&nbsp;&nbsp; // 第二階層對應第三階層<br />&nbsp;&nbsp;&nbsp; $('#mySecondChildSelect').cascade('#myFirstChildSelect', {<br />&nbsp;&nbsp;&nbsp; ajax: {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; type: &quot;post&quot;,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; url:&nbsp; 'action.php',<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; data: { act: 'second', val: $('#myFirstChildSelect').val() },<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;  <font color="#ff0000">dataType: &quot;json&quot;</font><br />&nbsp;&nbsp;&nbsp; },<br />&nbsp;&nbsp;&nbsp; template: function(item) { return &quot;&lt;option value='&quot; + item.Value + &quot;'&gt;&quot; + item.Text + &quot;&lt;/option&gt;&quot;; },<br />&nbsp;&nbsp;&nbsp; match: function(selectedValue) { return this.When == selectedValue; }<br />&nbsp;&nbsp;&nbsp; });<br />});<br />&lt;/script&gt;<br />&lt;/body&gt;<br />&lt;/html&gt;<br /><br />action.php：<br /><br />&lt;?php<br /><br />// 資料庫設定<br />$host_sql = &quot;localhost&quot;;<br />$username_sql = &quot;username&quot;;<br />$password_sql = &quot;password&quot;;<br /><br />$link = mysql_connect($host_sql, $username_sql, $password_sql) or die(&quot;無法連結資料庫&quot;);<br />mysql_select_db('target', $link);<br />mysql_query(&quot;SET NAMES UTF8&quot;);<br /><br />if (!empty($_GET['act'])) {<br />&nbsp;&nbsp;&nbsp; $action = $_GET['act'];<br />}<br /><br />if (!empty($_GET['val'])) {<br />&nbsp;&nbsp;&nbsp; $parentId = $_GET['val'];<br />}<br /><br /><font color="#ff0000">$list = array();</font><br /><br />switch ($action) {<br />&nbsp;&nbsp;&nbsp; case 'first':<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $query = &quot;SELECT id, name FROM table where lv = 2 AND parentid= $parentId&quot;;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $result = mysql_query($query, $link);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while ($row = mysql_fetch_assoc($result)) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <font color="#ff0000">$arr = array ('When' =&gt; $parentId, 'Value' =&gt; $row[&quot;ID&quot;], 'Text' =&gt; $row[&quot;NAME&quot;]);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $list[] = $arr;</font><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;<br />&nbsp;&nbsp;&nbsp; case 'second':<br />&nbsp;&nbsp;&nbsp; default :<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $query = &quot;SELECT id, name FROM table where lv = 3 AND parentid = $parentId&quot;;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $result = mysql_query($query, $link);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while ($row = mysql_fetch_assoc($result)) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <font color="#ff0000">$arr = array ('When' =&gt; $parentId, 'Value' =&gt; $row[&quot;ID&quot;], 'Text' =&gt; $row[&quot;NAME&quot;]);<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $list[] = $arr;</font><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;<br />}<br /><br /><font color="#ff0000">echo json_encode($list);</font><br /><br />哈，這樣就清楚多了呢！當然昨天用字串組的方式也可以囉，其所達到的效果是一樣的啦！提供另一種思考方向^^
		
		]]>
	</content:encoded>
	<link>http://blog.roodo.com/taikobo0/archives/6171055.html</link>
	<guid>http://blog.roodo.com/taikobo0/archives/6171055.html</guid>
	<category>AJAX</category>
	<pubDate>Fri, 13 Jun 2008 11:02:59 +0800</pubDate>
</item>
<item>
	<title>[AJAX] jQuery的多重下拉式選單應用</title>
	<description><![CDATA[
			P.S 2009-04-09 update：有更輕鬆簡單的方法，詳見[AJAX] jQuery的多重下拉式選單應用：Select box manipulation關於Ajax，我想最棒的地方就是可以於背景呼叫資料庫傳值吧～多重下拉式選單就是一項非常棒的Ajax應用；前幾天Jace丟過來一個國外的網址：jQuery.cascade : Cascading values from forms，這篇文章主要是在說明jQuery的cascade，而它就是用來實現多重下拉式選單的功能。花了一點時間實作了一下，發現非常簡單就能實現！以前我也作過類似的功能，可是花了我非常多的時間...jQuery把它包裝起來，讓一切變的簡單多了；以下是簡單的範例，給有需要的人參考吧：範例是三階層的關聯式多重下拉式選單，分為index.php（呈現頁）、action.php（Ajax後端資料處理頁）、以及jQuery的cascadeindex.php：&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;&lt;head&gt;&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot; /&gt;&lt;script type=&quot;text/javascript&quot; src=&quot;jquery.js&quot;&gt;&lt;/script&gt;&lt;script type=&quot;text/javascript&quot; src=&quot;jquery.cascade.js&quot;&gt;&lt;/script&gt;&lt;script type=&quot;text/javascript&quot; src=&quot;jquery.cascade.ext.js&quot;&gt;&lt;/script&gt;&lt;script type=&quot;text/javascript&quot; src=&quot;jquery.templating.js&quot;&gt;&lt;/script&gt;&lt;/head&gt;&lt;body&gt;第一項 &lt;select id=&quot;myParentSelect&quot;&gt;&lt;option value=&quot;&quot;&gt;請選擇&lt;/option&gt;&lt;?php&nbsp;&nbsp;&nbsp; // 資料庫設定&nbsp;&nbsp;&nbsp; $host_sql = &quot;localhost&quot;;&nbsp;&nbsp;&nbsp; $username_sql = &quot;username&quot;;&nbsp;&nbsp;&nbsp; $password_sql = &quot;password&quot;;&nbsp;&nbsp;&nbsp; $link = mysql_connect($host_sql, $username_sql, $password_sql) or die(&quot;無法連結資料庫&quot;);&nbsp;&nbsp;&nbsp; mysql_select_db('target', $link);&nbsp;&nbsp;&nbsp; mysql_query(&quot;SET NAMES UTF8&quot;);&nbsp;&nbsp;&nbsp; $query = &quot;SELECT id, name FROM table where lv = 1&quot;;&nbsp;&nbsp;&nbsp; $result = mysql_query($query, $link);&nbsp;&nbsp;&nbsp; while ($row = mysql_fetch_assoc($result)) {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; echo '&lt;option value=&quot;' . $row[&quot;id&quot;] . '&quot;&gt;' . $row[&quot;name&quot;] . '&lt;/option&gt;' . &quot;\n&quot;;&nbsp;&nbsp;&nbsp; }?&gt;&lt;/select&gt;　第二項 &lt;select id=&quot;myFirstChildSelect&quot;&gt;&lt;option value=&quot;&quot;&gt;請選擇&lt;/option&gt;&lt;/select&gt;第三項 &lt;select id=&quot;mySecondChildSelect&quot;&gt;&lt;option value=&quot;&quot;&gt;請選擇&lt;/option&gt;&lt;/select&gt;&lt;script type=&quot;text/javascript&quot;&gt;$(function () {&nbsp;&nbsp;&nbsp; // 第一階層對應第二階層&nbsp;&nbsp;&nbsp; $('#myFirstChildSelect').cascade('#myParentSelect', {&nbsp;&nbsp;&nbsp; ajax: {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; type: &quot;post&quot;,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; url:&nbsp; 'action.php',&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; data: { act: 'first', val: $('#myParentSelect').val() }&nbsp;&nbsp;&nbsp; },&nbsp;&nbsp;&nbsp; template: function(item) { return &quot;&lt;option value='&quot; + item.Value + &quot;'&gt;&quot; + item.Text + &quot;&lt;/option&gt;&quot;; },&nbsp;&nbsp;&nbsp; match: function(selectedValue) { return this.When == selectedValue; }&nbsp;&nbsp;&nbsp; });&nbsp;&nbsp;&nbsp; // 第二階層對應第三階層&nbsp;&nbsp;&nbsp; $('#mySecondChildSelect').cascade('#myFirstChildSelect', {&nbsp;&nbsp;&nbsp; ajax: {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; type: &quot;post&quot;,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; url:&nbsp; 'action.php',&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; data: { act: 'second', val: $('#myFirstChildSelect').val() }&nbsp;&nbsp;&nbsp; },&nbsp;&nbsp;&nbsp; template: function(item) { return &quot;&lt;option value='&quot; + item.Value + &quot;'&gt;&quot; + item.Text + &quot;&lt;/option&gt;&quot;; },&nbsp;&nbsp;&nbsp; match: function(selectedValue) { return this.When == selectedValue; }&nbsp;&nbsp;&nbsp; });});&lt;/script&gt;&lt;/body&gt;&lt;/html&gt;action.php：&lt;?php// 資料庫設定$host_sql = &quot;localhost&quot;;$username_sql = &quot;username&quot;;$password_sql = &quot;password&quot;;$link = mysql_connect($host_sql, $username_sql, $password_sql) or die(&quot;無法連結資料庫&quot;);mysql_select_db('target', $link);mysql_query(&quot;SET NAMES UTF8&quot;);if (!empty($_GET['act'])) {&nbsp;&nbsp;&nbsp; $action = $_GET['act'];}if (!empty($_GET['val'])) {&nbsp;&nbsp;&nbsp; $parentId = $_GET['val'];}$list = '[';switch ($action) {&nbsp;&nbsp;&nbsp; case 'first':&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $query = &quot;SELECT id, name FROM table where lv = 2 AND parentid= $parentId&quot;;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $result = mysql_query($query, $link);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while ($row = mysql_fetch_assoc($result)) {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $list .= '{\'When\':\'' . $parentId . '\',\'Value\':\'' . $row[&quot;id&quot;] . '\',\'Text\':\'' . $row[&quot;name&quot;] . '\'},';&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;&nbsp;&nbsp;&nbsp; case 'second':&nbsp;&nbsp;&nbsp; default :&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $query = &quot;SELECT id, name FROM table where lv = 3 AND parentid = $parentId&quot;;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $result = mysql_query($query, $link);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while ($row = mysql_fetch_assoc($result)) {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $list .= '{\'When\':\'' . $parentId . '\',\'Value\':\'' . $row[&quot;id&quot;] . '\',\'Text\':\'' . $row[&quot;name&quot;] . '\'},';&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;}$list .= ']';echo $list;實作的重點是在資料的格式：list = [{'When':'A1','Value':'W','Text':'SubchildA1a'},&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {'When':'A1','Value':'X','Text':'SubchildA1b'},&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; ];其中When代表上一階層的值，Value是此一階層的值，Test則是下拉式選單顯示的文字；要特別注意JavaScript是大小寫敏感的！實作這個範例途中，曾經被大小寫拖了一段時間...後來才發現～所以這地方要特別注意！希望大家都能輕鬆寫出關聯資料庫的多重下拉式選單囉^^
		]]>
	</description>
	<content:encoded><![CDATA[
			<strong>P.S 2009-04-09 update</strong>：有更輕鬆簡單的方法，詳見<a href="http://blog.roodo.com/taikobo0/archives/8671037.html" target="_blank">[AJAX] jQuery的多重下拉式選單應用：Select box manipulation</a><br /><br />關於Ajax，我想最棒的地方就是可以於背景呼叫資料庫傳值吧～多重下拉式選單就是一項非常棒的Ajax應用；前幾天Jace丟過來一個國外的網址：<a href="http://devlicio.us/blogs/mike_nichols/archive/2008/05/25/jquery-cascade-cascading-values-from-forms.aspx" target="_blank">jQuery.cascade : Cascading values from forms</a>，這篇文章主要是在說明<a href="http://plugins.jquery.com/project/cascade" target="_blank">jQuery的cascade</a>，而它就是用來實現多重下拉式選單的功能。<br /><br />花了一點時間實作了一下，發現非常簡單就能實現！以前我也作過類似的功能，可是花了我非常多的時間...jQuery把它包裝起來，讓一切變的簡單多了；以下是簡單的範例，給有需要的人參考吧：範例是三階層的關聯式多重下拉式選單，分為index.php（呈現頁）、action.php（Ajax後端資料處理頁）、以及<a href="http://plugins.jquery.com/project/cascade" target="_blank">jQuery的cascade</a><br /><br />index.php：<br /><br />&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;<br />&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;<br />&lt;head&gt;<br />&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot; /&gt;<br />&lt;script type=&quot;text/javascript&quot; src=&quot;jquery.js&quot;&gt;&lt;/script&gt;<br />&lt;script type=&quot;text/javascript&quot; src=&quot;jquery.cascade.js&quot;&gt;&lt;/script&gt;<br />&lt;script type=&quot;text/javascript&quot; src=&quot;jquery.cascade.ext.js&quot;&gt;&lt;/script&gt;<br />&lt;script type=&quot;text/javascript&quot; src=&quot;jquery.templating.js&quot;&gt;&lt;/script&gt;<br />&lt;/head&gt;<br />&lt;body&gt;<br />第一項 &lt;select id=&quot;myParentSelect&quot;&gt;<br />&lt;option value=&quot;&quot;&gt;請選擇&lt;/option&gt;<br />&lt;?php<br />&nbsp;&nbsp;&nbsp; // 資料庫設定<br />&nbsp;&nbsp;&nbsp; $host_sql = &quot;localhost&quot;;<br />&nbsp;&nbsp;&nbsp; $username_sql = &quot;username&quot;;<br />&nbsp;&nbsp;&nbsp; $password_sql = &quot;password&quot;;<br /><br />&nbsp;&nbsp;&nbsp; $link = mysql_connect($host_sql, $username_sql, $password_sql) or die(&quot;無法連結資料庫&quot;);<br />&nbsp;&nbsp;&nbsp; mysql_select_db('target', $link);<br />&nbsp;&nbsp;&nbsp; mysql_query(&quot;SET NAMES UTF8&quot;);<br /><br />&nbsp;&nbsp;&nbsp; $query = &quot;SELECT id, name FROM table where lv = 1&quot;;<br />&nbsp;&nbsp;&nbsp; $result = mysql_query($query, $link);<br />&nbsp;&nbsp;&nbsp; while ($row = mysql_fetch_assoc($result)) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; echo '&lt;option value=&quot;' . $row[&quot;id&quot;] . '&quot;&gt;' . $row[&quot;name&quot;] . '&lt;/option&gt;' . &quot;\n&quot;;<br />&nbsp;&nbsp;&nbsp; }<br /><br />?&gt;<br />&lt;/select&gt;　<br />第二項 &lt;select id=&quot;myFirstChildSelect&quot;&gt;<br />&lt;option value=&quot;&quot;&gt;請選擇&lt;/option&gt;<br />&lt;/select&gt;<br />第三項 &lt;select id=&quot;mySecondChildSelect&quot;&gt;<br />&lt;option value=&quot;&quot;&gt;請選擇&lt;/option&gt;<br />&lt;/select&gt;<br /><br />&lt;script type=&quot;text/javascript&quot;&gt;<br />$(function () {<br /><br />&nbsp;&nbsp;&nbsp; // 第一階層對應第二階層<br />&nbsp;&nbsp;&nbsp; $('#myFirstChildSelect').cascade('#myParentSelect', {<br />&nbsp;&nbsp;&nbsp; ajax: {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; type: &quot;post&quot;,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; url:&nbsp; 'action.php',<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; data: { act: 'first', val: $('#myParentSelect').val() }<br />&nbsp;&nbsp;&nbsp; },<br />&nbsp;&nbsp;&nbsp; template: function(item) { return &quot;&lt;option value='&quot; + item.Value + &quot;'&gt;&quot; + item.Text + &quot;&lt;/option&gt;&quot;; },<br />&nbsp;&nbsp;&nbsp; match: function(selectedValue) { return this.When == selectedValue; }<br />&nbsp;&nbsp;&nbsp; });<br /><br />&nbsp;&nbsp;&nbsp; // 第二階層對應第三階層<br />&nbsp;&nbsp;&nbsp; $('#mySecondChildSelect').cascade('#myFirstChildSelect', {<br />&nbsp;&nbsp;&nbsp; ajax: {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; type: &quot;post&quot;,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; url:&nbsp; 'action.php',<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; data: { act: 'second', val: $('#myFirstChildSelect').val() }<br />&nbsp;&nbsp;&nbsp; },<br />&nbsp;&nbsp;&nbsp; template: function(item) { return &quot;&lt;option value='&quot; + item.Value + &quot;'&gt;&quot; + item.Text + &quot;&lt;/option&gt;&quot;; },<br />&nbsp;&nbsp;&nbsp; match: function(selectedValue) { return this.When == selectedValue; }<br />&nbsp;&nbsp;&nbsp; });<br />});<br />&lt;/script&gt;<br />&lt;/body&gt;<br />&lt;/html&gt;<br /><br />action.php：<br /><br />&lt;?php<br /><br />// 資料庫設定<br />$host_sql = &quot;localhost&quot;;<br />$username_sql = &quot;username&quot;;<br />$password_sql = &quot;password&quot;;<br /><br />$link = mysql_connect($host_sql, $username_sql, $password_sql) or die(&quot;無法連結資料庫&quot;);<br />mysql_select_db('target', $link);<br />mysql_query(&quot;SET NAMES UTF8&quot;);<br /><br />if (!empty($_GET['act'])) {<br />&nbsp;&nbsp;&nbsp; $action = $_GET['act'];<br />}<br /><br />if (!empty($_GET['val'])) {<br />&nbsp;&nbsp;&nbsp; $parentId = $_GET['val'];<br />}<br /><br />$list = '[';<br /><br />switch ($action) {<br />&nbsp;&nbsp;&nbsp; case 'first':<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $query = &quot;SELECT id, name FROM table where lv = 2 AND parentid= $parentId&quot;;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $result = mysql_query($query, $link);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while ($row = mysql_fetch_assoc($result)) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $list .= '{\'When\':\'' . $parentId . '\',\'Value\':\'' . $row[&quot;id&quot;] . '\',\'Text\':\'' . $row[&quot;name&quot;] . '\'},';<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;<br />&nbsp;&nbsp;&nbsp; case 'second':<br />&nbsp;&nbsp;&nbsp; default :<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $query = &quot;SELECT id, name FROM table where lv = 3 AND parentid = $parentId&quot;;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $result = mysql_query($query, $link);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while ($row = mysql_fetch_assoc($result)) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $list .= '{\'When\':\'' . $parentId . '\',\'Value\':\'' . $row[&quot;id&quot;] . '\',\'Text\':\'' . $row[&quot;name&quot;] . '\'},';<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;<br />}<br /><br />$list .= ']';<br />echo $list;<br /><br />實作的重點是在資料的格式：<br /><br />list = [{'When':'A1','Value':'W','Text':'SubchildA1a'},<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {'When':'A1','Value':'X','Text':'SubchildA1b'},<br />&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; ];<br /><br />其中When代表上一階層的值，Value是此一階層的值，Test則是下拉式選單顯示的文字；要特別注意<font color="#ff0000">JavaScript是大小寫敏感的</font>！實作這個範例途中，曾經被大小寫拖了一段時間...後來才發現～所以這地方要特別注意！希望大家都能輕鬆寫出關聯資料庫的多重下拉式選單囉^^<strong><br /></strong><a href="http://blog.roodo.com/taikobo0/archives/8671037.html" target="_blank"></a>
		
		]]>
	</content:encoded>
	<link>http://blog.roodo.com/taikobo0/archives/6166625.html</link>
	<guid>http://blog.roodo.com/taikobo0/archives/6166625.html</guid>
	<category>AJAX</category>
	<pubDate>Thu, 12 Jun 2008 11:22:01 +0800</pubDate>
</item>
<item>
	<title>[AJAX] 初探jQuery</title>
	<description><![CDATA[
			因為哇寶在Ajax的應用上是採用jQuery，所以利用時間稍為了解一下。我以往在處理Ajax時大部分是借用網上的範例JS檔，所以它通常都有一套制式的規範，哪裡要new，哪裡要丟參數，範例上都寫得清清楚楚，只要按部就班，通常都能很快達到我想要的功能。另外一種是土法煉鋼，自己造輪子；先從JS丟參數到php，再由php切我要的參數去跟伺服端Query我要的資料，最後呈現。這二種方式其實好像都不是正統的方式說...在看完jQuery的介紹後更是讓我有這樣的感覺～網路上其實有很多淺顯易懂的介紹，我是看：國二學生認真打雜的jQuery 學習心得筆記一系列文章稍微接觸jQuery這個Framework的。詳盡且生動有趣的介紹，讓我對jQuery有粗淺的認識！有興趣可以去逛逛～^^
		]]>
	</description>
	<content:encoded><![CDATA[
			因為哇寶在Ajax的應用上是採用jQuery，所以利用時間稍為了解一下。<br /><br />我以往在處理Ajax時大部分是借用網上的範例JS檔，所以它通常都有一套制式的規範，哪裡要new，哪裡要丟參數，範例上都寫得清清楚楚，只要按部就班，通常都能很快達到我想要的功能。另外一種是土法煉鋼，自己造輪子；先從JS丟參數到php，再由php切我要的參數去跟伺服端Query我要的資料，最後呈現。這二種方式其實好像都不是正統的方式說...在看完jQuery的介紹後更是讓我有這樣的感覺～<br /><br />網路上其實有很多淺顯易懂的介紹，我是看：<a href="http://blog.ericsk.org/" target="_blank">國二學生認真打雜</a>的<a href="http://blog.ericsk.org/" target="_blank">jQuery 學習心得筆記</a>一系列文章稍微接觸jQuery這個Framework的。詳盡且生動有趣的介紹，讓我對jQuery有粗淺的認識！有興趣可以去逛逛～^^
		
		]]>
	</content:encoded>
	<link>http://blog.roodo.com/taikobo0/archives/5843793.html</link>
	<guid>http://blog.roodo.com/taikobo0/archives/5843793.html</guid>
	<category>AJAX</category>
	<pubDate>Fri, 11 Apr 2008 11:29:46 +0800</pubDate>
</item>
<item>
	<title>[AJAX] 自動完成autocomplete</title>
	<description><![CDATA[
			春節前在公司卡很久的自動完成，終於解套啦！（吼）會碰到這個問題，主要是因為本公司的專案管理系統，在材料成本新增時的廠商欄，會計反應會花很多時間在建立重複的資料上；一開始她建議使用下拉式選單...但是可想而知隨著資料量的增加，這下拉式選單也會變的愈來愈龐大...（汗）剛好前一陣子在研究自動完成，就想把這個應用加在專案管理系統中。關於自動完成，英文絕對沒什麼大問題；重點是中文啊～本來是要自己寫，真正動手以後才發現這個功能不是這麼簡單低。後來熊熊發現網路上有好用的免費資源啊！索性就先借用（？）來試試囉～起初非常驚訝，沒想到網路上的自動完成功能完善，畫面精美；簡單說就是一整個適合！不過是二個javascript跟一個css就把自動完成做的如此完美，用法也非常簡單！不過高興的情緒並沒有持續太久，我很快的發現到這個自動完成，在IE瀏覽器下對中文不支援！（英文OK）非常出乎意料的該網誌作者註明對firefox不支援...但是我的firefox卻OK；很殘念的會計所慣用的瀏覽器是IE6...就這樣自動完成又走進死胡同。後來我又花了不少時間在網上尋找適合的lib，期間不經意的發現原來一開始我所找到的免費資源其實是CAPXOUS的破解版；這壓根兒不是什麼免費資源！（汗）我所找到的是v1.3.0的版本，目前最新則是到v1.4.4版本。CAPXOUS本身也有提供程式碼下載以及解說～只不過它既然是個共享程式，廣告自然就少不了囉！最新的版本漂亮許多，當然付費授權的價格也就有點不親切...349 USD。v1.4.4版也只有幾個檔案，當然重點還是在autocomplete.js、prototype.js以及autocomplete.css上囉！有興趣的人可以直接從CAPXOUS的下載位置下載到，程式碼其實還頗複雜本人目前攻力尚淺，是處於一整個看不懂的階段，只好稍微解釋一下連結資料庫時的PHP檔，以及是如何運用在input欄位中的。主要使用自動完成的頁面，千萬別忘記先導入二個javascript跟一個css：（夾在&lt;head&gt;&lt;/head&gt;之間）&lt;script language=&quot;JavaScript&quot; src=&quot;javascripts/prototype.js&quot; type=&quot;text/JavaScript&quot;&gt;&lt;/script&gt;&lt;script language=&quot;JavaScript&quot; src=&quot;javascripts/autocomplete.js&quot; type=&quot;text/JavaScript&quot;&gt;&lt;/script&gt;&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;styles/autocomplete.css&quot; /&gt;廠商欄的輸入框：&lt;input name=&quot;supplier&quot; type=&quot;text&quot; id=&quot;supplier&quot; size=&quot;10&quot; /&gt;表格結束後的javascript，導入AJAX：&lt;script&gt;&nbsp;var d = new Date();&nbsp;new Autocomplete(&quot;supplier&quot;, function() {&nbsp; return &quot;Suggest.php?msec=&quot; + d.getMilliseconds() + &quot;&amp;type=0&amp;keyword=&quot; + this.value;&nbsp;});&lt;/script&gt;d：特別建立的時間值，利用d.getMilliseconds()取得毫秒後，用以防止瀏覽器在以後只讀快取中的資料而沒有重新查詢。supplier：input時的id（還是name忘記了...）type&amp;keyword：要傳入Suggest.php用以查詢資料庫連結資料庫用的Suggest.php：&lt;?php// 利用GET取得變數$keyword = $_GET['keyword'];$type = $_GET['type'];// 連線《Project》資料庫if($keyword != ''){&nbsp; $query = 'SELECT SUPPLIER ' .&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'FROM wh01_project_supplier ' .&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'WHERE SUPPLIER LIKE &quot;' . $keyword . '%&quot;' .&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'AND TYPE = &quot;' . $type . '&quot;';}// keyword無值時else{&nbsp; $query = 'SELECT SUPPLIER ' .&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'FROM wh01_project_supplier ' .&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'WHERE SUPPLIER=&quot;&quot;';}//&nbsp;執行 SQL query$result=mysql_query($query,$link);// 如果$result存在時if(!empty($result)&gt;0){&nbsp; while (list($SUPPLIER) = mysql_fetch_row($result)){&nbsp;&nbsp;&nbsp; echo &quot;&lt;li onselect=\&quot;this.text.value = '&quot;.$SUPPLIER.&quot;';\&quot;&gt;\n&quot;;&nbsp;&nbsp;&nbsp; echo $SUPPLIER.&quot;\n&quot;;&nbsp;&nbsp;&nbsp; echo &quot;&lt;/li&gt;\n&quot;;&nbsp; }}// 結束這個連線mysql_close();?&gt;從資料庫取出後只要把結果呈現成：&lt;li onselect=&quot;this.text.value = '結果'; &quot;&gt;&nbsp;結果&lt;/li&gt;就OK囉，最後請看成果。
		]]>
	</description>
	<content:encoded><![CDATA[
			<p>春節前在公司卡很久的自動完成，終於解套啦！（吼）<br /><br />會碰到這個問題，主要是因為本公司的專案管理系統，在材料成本新增時的廠商欄，會計反應會花很多時間在建立重複的資料上；一開始她建議使用下拉式選單...但是可想而知隨著資料量的增加，這下拉式選單也會變的愈來愈龐大...（汗）剛好前一陣子在研究自動完成，就想把這個應用加在專案管理系統中。關於自動完成，英文絕對沒什麼大問題；重點是中文啊～本來是要自己寫，真正動手以後才發現這個功能不是這麼簡單低。後來熊熊發現網路上有好用的<a href="http://www.dayanmei.com/blog.php/ID_652.htm" target="_blank">免費資源</a>啊！索性就先借用（？）來試試囉～<br /><br />起初非常驚訝，沒想到網路上的自動完成功能完善，畫面精美；簡單說就是一整個適合！不過是二個javascript跟一個css就把自動完成做的如此完美，用法也非常簡單！不過高興的情緒並沒有持續太久，我很快的發現到這個自動完成，在IE瀏覽器下對中文不支援！（英文OK）非常出乎意料的該網誌作者註明對firefox不支援...但是我的firefox卻OK；很殘念的會計所慣用的瀏覽器是IE6...就這樣自動完成又走進死胡同。<br /><br />後來我又花了不少時間在網上尋找適合的lib，期間不經意的發現原來一開始我所找到的<a href="http://www.dayanmei.com/blog.php/ID_652.htm" target="_blank">免費資源</a>其實是<a href="http://createwebapp.com/" target="_blank">CAPXOUS</a>的破解版；這壓根兒不是什麼免費資源！（汗）我所找到的是v1.3.0的版本，目前最新則是到v1.4.4版本。<a href="http://createwebapp.com/" target="_blank">CAPXOUS</a>本身也有提供程式碼下載以及解說～只不過它既然是個共享程式，廣告自然就少不了囉！最新的版本漂亮許多，當然付費授權的價格也就有點不親切...349 USD。<br /><br />v1.4.4版也只有幾個檔案，當然重點還是在autocomplete.js、prototype.js以及autocomplete.css上囉！有興趣的人可以直接從<a href="http://createwebapp.com/" target="_blank">CAPXOUS</a>的<a href="http://createwebapp.com/autocomplete.zip" target="_blank">下載位置</a>下載到，程式碼其實還頗複雜本人目前攻力尚淺，是處於一整個看不懂的階段，只好稍微解釋一下連結資料庫時的PHP檔，以及是如何運用在input欄位中的。<br /><br />主要使用自動完成的頁面，千萬別忘記先導入二個javascript跟一個css：（夾在&lt;head&gt;&lt;/head&gt;之間）<br /><br />&lt;script language=&quot;JavaScript&quot; src=&quot;javascripts/prototype.js&quot; type=&quot;text/JavaScript&quot;&gt;&lt;/script&gt;<br />&lt;script language=&quot;JavaScript&quot; src=&quot;javascripts/autocomplete.js&quot; type=&quot;text/JavaScript&quot;&gt;&lt;/script&gt;<br />&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;styles/autocomplete.css&quot; /&gt;<br /><br />廠商欄的輸入框：<br /><br />&lt;input name=&quot;supplier&quot; type=&quot;text&quot; id=&quot;supplier&quot; size=&quot;10&quot; /&gt;<br /><br />表格結束後的javascript，導入AJAX：<br /><br />&lt;script&gt;<br />&nbsp;var d = new Date();<br />&nbsp;new Autocomplete(&quot;supplier&quot;, function() {<br />&nbsp; return &quot;Suggest.php?msec=&quot; + d.getMilliseconds() + &quot;&amp;type=0&amp;keyword=&quot; + this.value;<br />&nbsp;});<br />&lt;/script&gt;<br /><br />d：特別建立的時間值，利用d.getMilliseconds()取得毫秒後，用以防止瀏覽器在以後只讀快取中的資料而沒有重新查詢。<br />supplier：input時的id（還是name忘記了...）<br />type&amp;keyword：要傳入Suggest.php用以查詢資料庫<br /><br />連結資料庫用的Suggest.php：<br /><br />&lt;?php<br />// 利用GET取得變數<br />$keyword = $_GET['keyword'];<br />$type = $_GET['type'];<br /></font></p><p>// 連線《Project》資料庫<br />if($keyword != ''){<br />&nbsp; $query = 'SELECT SUPPLIER ' .<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'FROM wh01_project_supplier ' .<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'WHERE SUPPLIER LIKE &quot;' . $keyword . '%&quot;' .<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'AND TYPE = &quot;' . $type . '&quot;';<br />}<br />// keyword無值時<br />else{<br />&nbsp; $query = 'SELECT SUPPLIER ' .<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'FROM wh01_project_supplier ' .<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'WHERE SUPPLIER=&quot;&quot;';<br />}<br />//&nbsp;執行 SQL query<br />$result=mysql_query($query,$link);<br />// 如果$result存在時<br />if(!empty($result)&gt;0){<br />&nbsp; while (list($SUPPLIER) = mysql_fetch_row($result)){<br />&nbsp;&nbsp;&nbsp; echo &quot;&lt;li onselect=\&quot;this.text.value = '&quot;.$SUPPLIER.&quot;';\&quot;&gt;\n&quot;;<br />&nbsp;&nbsp;&nbsp; echo $SUPPLIER.&quot;\n&quot;;<br />&nbsp;&nbsp;&nbsp; echo &quot;&lt;/li&gt;\n&quot;;<br />&nbsp; }<br />}<br />// 結束這個連線<br />mysql_close();</font></p><p>?&gt;<br /><br />從資料庫取出後只要把結果呈現成：<br /><br />&lt;li onselect=&quot;this.text.value = '結果'; &quot;&gt;<br />&nbsp;結果<br />&lt;/li&gt;<br /><br />就OK囉，最後請看<a href="http://taikobo.no-ip.org/CAPXOUS%20AutoCompleteversion%201.4.4/test.php" target="_blank">成果</a>。<br /></font></p>
		
		]]>
	</content:encoded>
	<link>http://blog.roodo.com/taikobo0/archives/5519565.html</link>
	<guid>http://blog.roodo.com/taikobo0/archives/5519565.html</guid>
	<category>AJAX</category>
	<pubDate>Sat, 09 Feb 2008 18:03:06 +0800</pubDate>
</item>
</channel>
</rss>