网页表单是什么
HTML forms are the bridge connecting users and backend servers.
We use forms to collect all kinds of user input: text, choices, comments, passwords, and send these data to the server for processing after submission.
表单用来收集各类用户输入:文字、选项、留言、密码,提交后把数据发给服务器做处理。
Core rule:Front form elements must match the parameters defined by backend program. 前端表单控件,必须和后端程序定义的参数一一对应。
The whole workflow: User fills in the form → clicks submit → browser packs data and sends request to server → server processes data and returns response page.
Core Attributes 核心属性
name
name 是后端接收的参数名。表单提交时,浏览器自动组装 name=value 键值对;控件如果没有 name,数据不会提交。
value
value 是真正传给后端的数据。页面展示给用户看到的文字,可以和代码里 value 的值不一样。
checked / selected
checked 用于单选框、复选框,页面加载时默认勾选。
selected 只用于下拉框<select>内部的<option>,设置默认选中项。
Common Form Elements 常用表单控件
1. Single-line text input 单行文本框
<input type="text" name="author" size="30">
用于简短文字输入,size 控制输入框可视宽度。
2. Password input 密码框
<input type="password" name="password" size="30">
页面上把输入内容隐藏为圆点。仅视觉遮蔽,传输过程没有加密。
3. Checkbox 复选框|Multiple selection
<input type="checkbox" name="sub" value="cs">Computer Science
多个复选框可以共用同一个 name,支持多选,后端会收到同一个参数名下多条值。
4. Radio button 单选框|Mutually exclusive choice
<input type="radio" name="studentType" value="u">Undergraduate
同一组选项必须同名,同一时间只能选择一项,互斥选择。
5. Select dropdown 下拉选择框
<select name="day">
<option value="fri" selected>Friday</option>
</select>
包裹多个 option 标签。加上multiple属性开启多选下拉,按住 Ctrl 可以多选。
6. Textarea 多行文本域
<textarea name="comment" rows="5" cols="30"></textarea>
适合大段文字输入。rows 是可见行数,cols 是可见字符宽度;内容写在标签中间,不是放在 value 属性。
7. Buttons 按钮
<input type="submit">触发表单提交,按钮文字由 value 设置。<input type="reset">重置表单,清空所有输入,恢复页面初始状态。
GET vs POST 两种提交方式对比
Defined in <form method="get/post">, together with action which is the target server URL.
| GET | POST |
|---|---|
| Data appended after URL, visible in address bar. 数据拼接在 URL 后,地址栏直接可见 | Data carried inside request body, hidden from URL. 数据放在请求体,不在地址栏展示 |
| Saved in browser history, can add to bookmarks. 保存在浏览历史,支持收藏书签 | Not stored in history, cannot bookmark directly. 不存入历史记录,无法直接书签保存 |
| URL length limit (max ~2048 chars), only ASCII characters. URL 长度上限约 2048 字符,仅支持 ASCII 字符 | No strict length limit, supports binary data like file upload. 无严格长度限制,支持二进制,可上传文件 |
| Low security. Never send password or private data via GET. 安全性差,严禁传输密码等敏感信息 | More suitable for sensitive information. 更适合提交隐私敏感数据 |
| Back or reload won’t cause repeated submission risk. 回退、重载一般无重复提交风险 | Reload will trigger resubmit warning from browser. 页面重载,浏览器会提示重复提交 |
GET相对来说更简单 不加密,仅仅是把数据放在链接上,本身没有安全保护。
搜索类表单大多使用 GET,参数示在 URL;登录表单多用 POST,参数藏在请求体中。
Full Demo Code 完整演示代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Whosville Library Search Demo</title>
</head>
<body>
<h2>Whosville Library Book Search</h2>
<form action="http://library.whosville/bsearch" method="get">
Author: <input type="text" name="author" size="30"/><br>
Year: <input type="text" name="year" size="10"/><br>
Subjects (Multiple Choice):
<input type="checkbox" name="sub" value="mth">Mathematics
<input type="checkbox" name="sub" value="cs" checked>Computer Science
<input type="checkbox" name="sub" value="bio">Biology
<input type="checkbox" name="sub" value="phy">Physics
<input type="checkbox" name="sub" value="chem">Chemistry<br>
Student Type:
<input type="radio" name="studentType" value="u">Undergraduate
<input type="radio" name="studentType" value="p" checked>Postgraduate
<input type="radio" name="studentType" value="other">Other<br>
Weekday:
<select name="day">
<option value="mon">Monday</option>
<option value="tue">Tuesday</option>
<option value="wed">Wednesday</option>
<option value="thu">Thursday</option>
<option value="fri" selected>Friday</option>
</select><br>
Multiple Days Selection:
<select name="days" multiple>
<option value="mon">Monday</option>
<option value="fri">Friday</option>
</select><br>
Comment:<br>
<textarea name="comment" rows="5" cols="30"></textarea><br>
Password: <input type="password" name="password" size="30"/><br>
<input type="submit" value="Search">
<input type="reset" value="Reset form">
</form>
</body>
</html>
1467



被折叠的 条评论
为什么被折叠?



