代碼示例
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>注冊</title>
</head>
<body>
<form action="http://localhost:8080/html/register" method="post">
用戶名
<input type="text" name="username" /><br>
密碼
<input type="password" name="userpwd" /><br>
確認密碼
<input type="password" /><br>
性別:
男<input type="radio" name="sex" value="男" />
女<input type="radio" name="sex" value="女" /><br>
興趣愛好:
抽煙<input type="checkbox" name="hobby" value="smoke" />
喝酒<input type="checkbox" name="hobby" value="drink" />
燙頭<input type="checkbox" name="hobby" value="perm" /><br>
學歷:
<select name="grade">
<option value="z">專科</option>
<option value="b" selected>本科</option>
<option value="y">研究生</option>
</select><br>
簡介:<br>
<!--文本域沒有value屬性,用戶填寫的就是value-->
<textarea rows="10" cols="50" name="introduction"></textarea><br>
<input type="submit" value="注冊" />
<input type="reset" value="清空" />
</form>
</body>
</html>
form表單method屬性:
1、get:采用get方式提交的時候,用戶提交的信息會顯示在瀏覽器的地址欄上。
<form action="http://localhost:8080/html/register" method="get">
地址欄顯示:
http://localhost:8080/html/register?username=123&userpwd=123&sex=%E7%94%B7&hobby=smoke&hobby=drink&hobby=perm&grade=b&introduction=123
2、post:采用post方式提交的時候,用戶提交的信息不會顯示在瀏覽器地址欄上。
<form action="http://localhost:8080/html/register" method="post">
3、當用戶提交的信息中含有敏感信息,例如:密碼。建議采用post方式提交。
4、method屬性不指定,或者指定get,這種情況下都是get。
只有當method屬性指定為post的時候才是post請求。剩下所有的請求都是get請求。(默認為get)
5、post提交的數據格式和get還是一樣的,只不過不在地址欄上顯示出來。