2008年4月30日

HTML Form

我把一些經常使用的HTML form規格與參數給記下來



  1. Hidden Elements
    <input type=hidden name="apple" value="fruit">
    使用者不會看到任何的東西,但是當表單送出時,會傳送apple=fruit這個值。
  2. Text Elements

    <input type=text name="pie_filling" value="">
    顯示出一行可輸入文字的文字框,若使用者輸入cherry,則會傳送pie_filling=cherry這個值。
  3. Password Elements

    <input type=password name="pie_filling" value="cherry">
    同上是可輸入文字的文字框,但是輸入的文字會以.取代,傳送數值時,仍是以明碼傳送。
  4. Checkboxes

    <input type=checkbox name="pie_filling" value="choose">
    使用者無法改變value之值,若是使用者選取了此選項,則會傳送pie_filling=choose之值。若在設計時沒有設定value之值,則在傳送時會送出name=on。
    若要一開始就預設選取,可在最後面加上checked,如下:

    <input type=checkbox name="default" value="choose" checked>
  5. Radio Buttons
    常用於多選一的情形,和checkboxes類似,在選取raido buttons其中一項時,同名的buttons會從on<->off。



    <input type=radio name="test" value=1 checked>
    <input type=radio name="test" value=2>
    <input type=radio name="test" value=3>
  6. Submit Buttons

    <input type=submit value="Go" name="test">
    按下按鈕時,會送出form,value之值是顯示在button上面的文字。

    也可用同名的buttons來辨別不同的動作,如下:
    <input type=submit name="what_to_do" value="Continue shopping">
    <input type=submit name="what_to_do" value="Check out">
    <input type=submit name="what_to_do" value="Erase order">

    若是Form有action屬性時,在按下submit時,會把form裡面的參數傳給action所指定的url。
    <form type=get action="searcher.cgi">
    <input type=hidden name="session" value="3.14169">
    <input type=text name="key" value="">
    <input type=submit name="verb" value="search">
    </form>
    會將session=3.14169, key=chen, verb=search傳給searcher.cgi。
  7. Image Buttons
    <input type=image name="img_btn" src="test.gif">
    和submit相同,只是顯示在按鈕上面從文字變成了圖片,用src指定圖片的路徑。
    Image button還可以知道使用者按到了圖片的那一部份,在按鈕中,圖片的最左上方的坐標值為(0,0),也就是若使用者是按左上方送出時,會傳送img_btn.x=0, img_btn.y=0值。若在一開始時沒有指定name的名稱時,傳送時慣例是用x=0,y=0的方式送出。
  8. Reset Buttons

    <input type=reset value="nevermind>
    清除form的內容且不送出任何資料,返回form原始的狀態,value之值為顯示在按鈕上的文字。
  9. Textarea Elements

    <textarea name="pairname">Defalut content, first line.
    Another Line.
    The last Line.</textarea>
    使用者可以輸入多行文字。
  10. Select Elements and Option Elements
    下拉式選單

    <select name="flavor">
    <option value="cherry">Cherry</option>
    <option value="vanilla" selected>Vanilla</option>
    <option value"lemon">Lemon</option>
    </select>
    當選擇Cherry時,會送出flavor=cherry這個值。屬性selected代表預設選項。
    若是opton沒有設定value之值時,會以在<option></option>之間的文字當做值送出,如下:
    <option>apple</option> 等同於
    <option value="apple">apple</option>


    <select name="flavor" multiple>
    <option value="cherry">Cherry</option>
    <option value="vanilla">Vanilla</option>
    <option value="lemon">Lemon</option>
    </select>
    當加上multiple屬性時,可讓使用者多選,但較少用。

2008年4月19日

Web Basics Digest

  • URL(RFC 2396)

    scheme://username@server:port/path?query

    scheme和hostname的部份是不分大小寫的,但是其它的部份大小寫是不同的。

    URL可使用的字元是ASCII a-z, A-Z, 0-9和: @ & + $ ( ) /這些符號,其它的字元必須被編碼才行。(Ex:space = %20)

  • HTTP Request(RFC 2616)

    GET /light_button.png HTTP/1.1" 200 4005
    Host: www.yahoo.com.tw
    user-Agent:Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-TW; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13
    [blank line]

    HTTP Request有三個部份:1、Request Line  2、Header  3、Body of the request(用於傳遞form的參數)

    Request Line是客戶端用來說明想要什麼東西,常用的有GET和POST兩種方法。

    Header為一連串的key/value,在HTTP/1.1中,Host為必要的header,其它的header不一定會被使用。Header最後必需以一空白列當做結束。

    message可傳遞任意資料,如果message中有資料,則Content-Type和Content-Length兩個 headers會被設定。使用GET queries時,不會有任何的附加資料,所以message部份為空,而用POST queries時,會使用message部份來傳遞參數(資料)。

  • HTTP Response

    HTTP Resonpse也有三個部份:1、 Status Line   2、Headers   3、Optional Body

    Status Line說明Server是用何種通訊協定,包含了Status Code和簡短的訊息。 (100s:informational, 200s:Successful, 300s:Redirection, 400s:Client Error, 500s:Server Error)

    Header部份讓Server在response中附加更多額外的資訊,最後也是以一空白列做為結尾。

    body中可為任何資料,在一般的Request中,為html文件。若發生錯誤時,body是傳回由Server產生的 Error message。

2008年4月16日

安裝lighttpd for Windows with Perl

    我的目的是要在Windows上面測試用Perl寫的cgi程式,因為不想用IIS,Apache又有一點肥,剛好在網路上搜尋時,看到了Lighttpd for Windows版本,是由匈牙利人所開發的套件,於是就決定抓下來試試。

  1. 下載Lighttpd

    有兩個版本可以選擇,因為只是要測試用,所以選沒有SSL support的那個套件會比較快,Download site選在USA的mirror站下載會快一點。

  2. 安裝並啟動Lighttpd

    預設是安裝在C:\Program Files\LightTPD,安裝完成後,執行bin\Service-Install.exe,會把Lighttpd安裝成Windows Service。

    完成後,開啟Browser,網址輸入http://localhost,若是有出現Lighttpd的網頁,表示已經安裝成功。

  3. 設定Lighttpd

    在此之前,電腦要先裝好Perl套件,我用的是ActivePerl 5.10.0版,預設是安裝在C:\Perl。

    到C:\Program Files\LightTPD\conf中編輯lighttpd-inc.conf,這個檔案是Lighttpd的主要設定檔,設定如下:

    server.modules = ( 
                                    "mod_access", 
                                    "mod_accesslog", 
                                    "mod_alias",    
                                    "mod_cgi" #將這一行前面的註掉
                              )

    #這邊一定要用絕對路徑,否則在測試CGI時會出現500 Internal Error
    server.document-root  = "C:/Program Files/LightTPD/htdocs/"

    #這邊要把註解拿掉,並且將perl設定至正確的路徑,注意反斜線
    cgi.assign                  = ( ".php" => "PHP/php-cgi.exe",
                                         ".pl"  => "C:/perl/bin/perl.exe",
                                         ".cgi" => "C:/perl/bin/perl.exe"
                                       )

  4. 測試CGI

    在C:\Program Files\LightTPD\htdocs中建立cgi-bin資料夾,我們將cgi程式放在此。
    用下面這一支程式測試,儲存成test.pl放在cgi-bin資料夾中。

    #!/usr/bin/perl
    print "Content-type:text/html\n\n";
    print "Hello ...\n";

    然後用http://localhost/cgi-bin/test.pl測試,若是出現hello文字,表示設定成功了。smile_teeth