PHP 後端處理表單提交是基礎核心技能,熟悉 HTML 表單屬性、輸入類型、編碼規則與檔案上傳細節,能避免大量常見 Bug。本文系統整理表單核心知識點。
<form name="form1" id="form1" method="GET|POST" action="提交URL"
target="_blank|_parent|_self|_top" enctype="multipart/form-data">
<!-- 表單元素 -->
</form>
GET 參數顯於 URL(長度限制、可書籤),POST 隱於請求主體(無長度限制、檔案上傳必須用 POST)application/x-www-form-urlencoded(預設):一般表單資料multipart/form-data:檔案上傳必須指定text/plain:極少用_blank 新分頁、_self 當前頁(預設)<input type="text" name="" id="" value="" class="" /> <input type="password" name="" id="" value="" class="" /> <input type="hidden" name="" id="" value="" class="" />
<input type="file" name="" id="" value="" class="" accept="" />
image/*、image/jpeg、image/png、image/gifapplication/msword、application/pdf、application/vnd.ms-excel、application/zipaudio/*、audio/mpeg、audio/wavvideo/*、video/mp4、video/quicktimeaccept="image/jpeg, image/png, application/pdf"<input type="radio" name="group" value="1" checked="checked" /> <input type="checkbox" name="items[]" value="1" checked="checked" /> <select name="single"><option value="1" selected>選項</option></select> <select name="multi[]" multiple size="5"><option value="1">選項</option></select> <textarea name="desc" rows="5" cols="40">內容</textarea>
<input type="submit" value="提交" /> <input type="reset" value="重置" /> <input type="button" value="按鈕" /> <input type="image" src="btn.png" />
$_GET、$_POST:對應 method 取得一般欄位$_FILES:檔案上傳資訊(name、tmp_name、size、error、type)name="items[]" → $_POST['items'] 為陣列id 屬性在頁面唯一,name 可重複(單選/核取方塊分組)&、=、?、# 等特殊字元在 URL 參數中需編碼urlencode()、rawurlencode()(RFC 3986,空格編為 %20)urldecode()、rawurldecode()encodeURIComponent()、decodeURIComponent()$_FILES 為空isset() 判斷掌握表單細節,能讓後端驗證、資料綁定、檔案處理更順暢,減少前後端溝通成本與隱性 Bug。