本章涵蓋 <FORM>、<INPUT>、<SELECT>、<OPTION>、<TEXTAREA>,其中 INPUT 的 type 可分為 Text、Radio、Checkbox、Password、Submit/Reset、Image、File、Hidden、Button 等類型。
表單在網上無處不在,通常是搭配 CGI 程式使用。若你打算深入 CGI,表單設計是必修課。本章標記數量不多,但參數變化相當多。
一份表單的基本架構是:在 <FORM> 標記的包圍下,放入一種或以上的輸入方式,以及一個或以上的按鈕。
例如:<form action="https://your.isp.com/cgi-local/example.cgi" method="POST">
action="mailto:your@email.com",此時表單資料會寄到該信箱。由於 type 的選擇很多,且其他參數會隨 type 而異,以下按輸入類型分別說明。
<input type="Text" name="age" value="20" align="MIDDLE" size="2" maxlength="255">
Your_Age=40。例子:
<form action="https://your.isp.com/cgi-local/example.cgi" method="POST">
請填入電話號碼:<input type="Text" name="phone" value="" size="10" maxlength="8">
</form>
<input type="Radio" name="gender" value="female" align="MIDDLE" checked>
例子:
<form action="https://your.isp.com/cgi-local/example.cgi" method="POST">
請選性別:
<input type="Radio" name="gender" value="Female">女性
<input type="Radio" name="gender" value="Male" checked>男性
<br>你喜歡嗎:
<input type="Radio" name="like" value="Yes">喜歡
<input type="Radio" name="like" value="No">不喜歡
<input type="Radio" name="like" value="NotSure">不肯定
</form>
現代補充:<FORM> 的 action 與 method 依然是標準做法,但表單驗證已不必只靠伺服器。HTML5 提供了 required、type="email"、type="tel"、pattern 等原生屬性,能直接在瀏覽器端完成基本檢查;每個輸入欄位也建議用 <label> 明確關聯(<label for="phone">),這對可訪問性與點擊範圍都有實質幫助。