[分享]解決表單亂碼問題

WOG設定安裝教學

版主: 涅魂, 簫哥


涅魂
 
文章: 4463
註冊時間: 2004-01-04 11:17 am
來自: Taiwan
性別: 男生

[分享]解決表單亂碼問題

文章涅魂 » 2007-03-15 11:17 pm

經過長時間的研究終於找出不用mbstring模組的解決方法了 :D

這個問題的解決方法是將javascript中要送出的表單複製一份到foot內的表單(f2)後再送出,點子是從wog中的act_click()和foot_trun()來的

不過為了傳送內容的正確性加上了許多判斷而犧牲了速度,所以若是可以用act_click()或foot_trun()取代的話盡量不要使用form_send()以增快處理速度

以下列出form_send()的程式碼(BUG已修正)
代碼: 選擇全部
function form_send(f)
{
   var frm=parent.foot.document.f2;
   while(i=frm.children[0])
   frm.removeChild(i);
   var len=f.elements.length;
   var btn_log=new Array();
   for(var l=0;l < len;l++)
   {
      var temp_s="";
      var btn_check=false;
      var i=f.elements[l];
      var btn_name=i.name;
      if(btn_name == "")continue;
      for(var n=0;n < btn_log.length;n++)
      {
         if(btn_name == btn_log[n])
         {
            btn_check=true;
            break;
         }
      }
      if(btn_check == false)
      {
         var btn_tagname=(i.tagName).toLowerCase();
         if(btn_tagname=="select")btn_tagname="input";
         var btn=parent.foot.document.createElement(btn_tagname);
         var btn_type=(i.type).toLowerCase();
         btn.name=btn_name;
         //btn.type="hidden";
         if(btn_type == "radio" || btn_type == "checkbox")
         {
            var item=f.all(btn_name);
            for(var j=0;j<item.length;j++)
            {
               if(item[j].checked==true)
               {
                  temp_s+=","+item[j].value;
               }         
            }
            temp_s=temp_s.substr(1,temp_s.length);
            btn.value=temp_s;
         }else
         {
            btn.value=i.value;
         }
         frm.appendChild(btn);
         btn_log.push(btn_name);
      }
   }
   frm.action="wog_act.php";
   frm.method="POST";
   frm.target="mission";
   frm.submit();
}

然後在wog_foot.htm裡面加上f2即可使用
代碼: 選擇全部
<form action="wog_act.php" method="POST" target="mission" name="f2" style="display:none"></form>


功能說明

安裝說明與範例
以上,有任何問題請指教。 :wink:


最後由 涅魂 於 2007-04-08 3:05 pm 編輯,總共編輯了 6 次。


涅魂
 
文章: 4463
註冊時間: 2004-01-04 11:17 am
來自: Taiwan
性別: 男生

文章涅魂 » 2007-03-16 11:10 pm

發現bug,下列為修正後的函式與foot的表單內容,以紅字標示修改部分
代碼: 選擇全部
function form_send(f)
{
   var frm=parent.foot.document.f2;
   while(i=frm.children[0])
   frm.removeChild(i);
   var len=f.elements.length;
   var btn_log=new Array();
   for(var l=0;l < len;l++)
   {
      var temp_s="";
      var btn_check=false;
      var i=f.elements[l];
      var btn_name=i.name;
      [color=red]if(btn_name == "")continue;[/color]
      for(var n=0;n < btn_log.length;n++)
      {
         if(btn_name == btn_log[n])
         {
            btn_check=true;
            break;
         }
      }
      if(btn_check == false)
      {
[color=red]         var btn_tagname=(i.tagName).toLowerCase();
         if(btn_tagname=="select")btn_tagname="input";
         var btn=parent.foot.document.createElement(btn_tagname);[/color]
         var btn_type=(i.type).toLowerCase();
         btn.name=btn_name;
         [color=red]//btn.type="hidden";[/color]
         if(btn_type == "radio" || btn_type == "checkbox")
         {
            var item=f.all(btn_name);
            for(var j=0;j<item.length;j++)
            {
               if(item[j].checked==true)
               {
                  temp_s+=","+item[j].value;
               }         
            }
            temp_s=temp_s.substr(1,temp_s.length);
            btn.value=temp_s;
         }else
         {
            btn.value=i.value;
         }
         frm.appendChild(btn);
         btn_log.push(btn_name);
      }
   }
   frm.action="wog_act.php";
   frm.method="POST";
   frm.target="mission";
   frm.submit();
}

代碼: 選擇全部
<form action="wog_act.php" method="POST" target="mission" name="f2" [color=red]style="display:none"[/color]></form>



圖檔

頭像
齋月
 
文章: 557
註冊時間: 2006-02-27 7:54 am

文章齋月 » 2007-03-17 8:43 am

表單亂碼是什麼意思? 不太懂 0.0




涅魂
 
文章: 4463
註冊時間: 2004-01-04 11:17 am
來自: Taiwan
性別: 男生

文章涅魂 » 2007-03-17 7:56 pm

嗯,沒有附上說明果然不容易理解^^"

簡單來說,這個問題就跟之前月分享的線上狀態那篇所遇上的亂碼問題一樣的

以這個例子來說,這是一般情況時直接使用html語法設計的表單
代碼: 選擇全部
<form action="wog_act.php" method="POST" target="mission" name="f1">
<input type="text" name="test_name" value="測試文字">
<input type="submit" value="送出">
</form>

這是常見的送出表單的例子(使用submit來送出),這在一般情況下使用沒有問題,繁體字可以正常送出

但是若在wog的wog.js中這種寫法卻會使得test_name的值變成亂碼,wog.js中的寫法如下
代碼: 選擇全部
var f=parent.wog_view.document;
f.write('<form action="wog_act.php" method="POST" target="mission" name="f1">');
f.write('<input type="text" name="test_name" value="測試文字">');
f.write('<input type="submit" value="送出">');
f.write('</form>');

送出後的結果如下,警告視窗中為PHP程式中所收到的$_POST['test_name']的值
圖檔
這個問題我以前在設計工會頭銜與工會投票時都有碰過,工會頭銜是把步驟複雜化來解決,工會投票則使用mbstring模組

但這都不是好的解決辦法,有用過工會頭銜與工會投票外掛的朋友應該會覺得操作過程過於繁複而多餘,這都是為了解決亂碼問題而採取的下下策

wog中雖然可以用act_click()與foot_trun()來解決這個問題,但這卻只能同時傳送一筆資料,要增加的話必須要擴充這兩個函式,但這又會使得程式沒有彈性,用 , 來連結數筆資料也不是個好辦法

而這也就是我研究這個的原因了^^

(這麼說來我是不是該把這兩個外掛好好地整修一翻了XD?)


圖檔

頭像
齋月
 
文章: 557
註冊時間: 2006-02-27 7:54 am

文章齋月 » 2007-03-18 7:39 am

原來如此~~ 我都不知道是因為這樣的問題 XDD
只知道涅幫忙修好了 :oops:


技術還不夠成熟... 此文章內容不會修改 XP
我懂太少了 :face6:

如果涅有空的話 將兩個外掛翻修會非常感激的 :wink:
(也可以當作實際用法... 我都是從你們各位高手的實際修改中學習的 :oops: )

看書看的霧煞煞 :face8:

=======================================
學業大危機ing =.=




涅魂
 
文章: 4463
註冊時間: 2004-01-04 11:17 am
來自: Taiwan
性別: 男生

文章涅魂 » 2007-03-18 11:56 am

使用方法也很簡單,跟平常用慣的act_click()與foot_trun()其實一樣喔,只是傳入值只有一個表單而已^^

安裝方法如下

開啟檔案wog/wog.js

尋找
代碼: 選擇全部
function th_submit(b,s,a)
{
   var thisfrom=b;
   thisfrom.temp_id2.value=s;
   thisfrom.temp_id.value=a;
   thisfrom.submit();
}

下面,加上
代碼: 選擇全部
請參照首文的function form_send(f)


開啟檔案wog/wog_foot.htm

尋找
代碼: 選擇全部
</form>

下方,加上
代碼: 選擇全部
<form action="wog_act.php" method="POST" target="mission" name="f2" style="display:none"></form>

這樣就能使用form_send()了^^

再附上個使用範例

假設這是wog.js中某個function
代碼: 選擇全部
function group_set_view(peo_max)
{
   var f=parent.wog_view.document;
   message_cls();
   f.write('<form action="wog_act.php" method="POST" target="mission" [color=red]name="f2"[/color]>');
   f.write(temp_table1);
   f.write('<tr><td colspan="3" bgcolor="#3B588E">工會設定</td></tr>');
   f.write('<tr><td width="20%" bgcolor="#4B689E">工會人數上限</td><td width="65%"><input type="text" name="peo_max" value="'+peo_max+'" size="6">人 (不限人數請設置為0人)</td><td width="15%"><input type="button" value="設定" [color=red]onclick="parent.form_send(document.f2)"[/color]></td></tr>');
   [color=red]f.write('<input type="hidden" name="f" value="group"><input type="hidden" name="act" value="set">');[/color]
   f.write(temp_table2);
   f.write('</form>');
}

其中的紅字是設計時的重點,表單要設定名稱以取得正確的表單、送出的事件換成parent.form_send()且傳送的值為表單資料、加上隱藏值來傳送f 與 act

這樣wog_act.php就能收到$_POST['f'] $_POST['act'] $_POST['peo_max']這三個值了,是不是很簡單呢^^?
齋月 寫:學業大危機ing =.=

不會是快被當掉了吧 ? :shock:


圖檔

頭像
齋月
 
文章: 557
註冊時間: 2006-02-27 7:54 am

文章齋月 » 2007-03-18 1:10 pm

涅魂 寫:教學太棒了 被我全部吃掉了 XDD

齋月 寫:學業大危機ing =.=

不會是快被當掉了吧 ? :shock:


英文已經被當兩次 浪費整整一年時間了  :face8:
目前第三修.......




涅魂
 
文章: 4463
註冊時間: 2004-01-04 11:17 am
來自: Taiwan
性別: 男生

文章涅魂 » 2007-03-18 1:21 pm

呃,英文被當掉的話月你怎麼想到加拿大讀書呢@@?

還是說你們是在讀更難的英文?像古典詩集之類的嗎?


圖檔

頭像
齋月
 
文章: 557
註冊時間: 2006-02-27 7:54 am

文章齋月 » 2007-03-18 2:27 pm

讀書當然是為了好學業囉~~
以後可以有好一點的BACKGRUOND

難的話...可能是我太差吧 哈哈~~
語言方面我都是很不行的 囧TZ




回到 精華區

誰在線上

正在瀏覽這個版面的使用者:沒有註冊會員 和 4 位訪客