<form name="form1" id="form1" method="GET|POST" action="提交URL" target="_blank|_parent|_self|_top" enctype="multipart/form-data">
</form>
GET 參數在 URL,長度受限、可收藏、不安全;POST 數據在請求體,無長度限制、適合敏感/大數據提交。application/x-www-form-urlencoded(默認):鍵值對編碼。multipart/form-data:**文件上傳必須使用**,且 method 必須為 POST。text/plain:極少用。| 類型 | 代碼示例 | 關鍵屬性/說明 |
|---|---|---|
| 文本框 | <input type="text" name="username" id="username" value="" class=""> | 單行文本,配合 maxlength、pattern 驗證 |
| 密碼框 | <input type="password" name="pwd" id="pwd"> | 顯示為圓點,傳輸仍為明文(需 HTTPS) |
| 文件上傳 | <input type="file" name="avatar" id="avatar" accept="image/*"> | 必須 POST + multipart/form-data;accept 限制 MIME 類型 |
| 圖片按鈕 | <input type="image" name="submit" src="/btn.png"> | 提交表單並傳送點擊座標 name.x/name.y |
| 單選框 | <input type="radio" name="sex" value="1" checked> 男 | 同 name 為一組,單選 |
| 復選框 | <input type="checkbox" name="hobby[]" value="reading" checked> 讀書 | name 以 [] 結尾,PHP 接收為數組 |
| 提交按鈕 | <input type="submit" value="提交"> | 觸發提交 |
| 重置按鈕 | <input type="reset" value="重置"> | 恢復表單初始值 |
| 普通按鈕 | <input type="button" value="按鈕" onclick="..."> | 無默認行為,配合 JS |
| 隱藏域 | <input type="hidden" name="csrf_token" value="abc123"> | 不顯示,隨表單提交 |
| 下拉單選 | <select name="city"><option value="bj" selected>北京</option></select> | |
| 下拉多選 | <select name="city[]" multiple>...</select> | name[] + multiple,PHP 接收數組 |
| 多行文本 | <textarea name="desc" rows="5" cols="40">內容</textarea> | 無 value 屬性,內容放標籤內 |
accept 常用 MIME 類型速查// 圖片
image/*、image/jpeg、image/png、image/gif、image/webp
// 文檔
application/pdf、application/msword、application/vnd.openxmlformats-officedocument.wordprocessingml.document
application/vnd.ms-excel、application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
// 壓縮包
application/zip、application/x-rar-compressed、application/x-7z-compressed
// 視頻/音頻
video/mp4、video/webm、audio/mpeg、audio/mp3
// 多類型寫法
<input type="file" accept="image/*,application/pdf">
// 單文件
$file = $_FILES['avatar'];
/* 結構:
['name'] => 原文件名
['type'] => MIME 類型(瀏覽器提供,不可信)
['tmp_name'] => 臨時存儲路徑
['error'] => UPLOAD_ERR_OK(0) 等錯誤碼
['size'] => 字節大小
*/
// 多文件 (name="files[]")
foreach ($_FILES['files']['tmp_name'] as $idx => $tmp) {
if ($_FILES['files']['error'][$idx] === UPLOAD_ERR_OK) {
move_uploaded_file($tmp, "/uploads/{$_FILES['files']['name'][$idx]}");
}
}
? & = % # 等)時,**發送前必須 urlencode()**;$_GET/$_POST,若手動構造 URL 需 urldecode();rawurlencode() / rawurldecode() 按 RFC 3986(空格編碼為 %20 而非 +),**API 簽名、OAuth 必須用 raw 版**。頁面中 id 屬性**全局唯一**,name 可重複(如單選/復選組)。JS document.getElementById()、label for、CSS #id 選擇器均依賴唯一性。