Front-End/HTML

[HTML] 08. form(ํผ)

Bigone17 2021. 12. 6. 21:55

๐ŸŸก 1. form ํƒœ๊ทธ

 - form ํƒœ๊ทธ๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ์›น์‚ฌ์ดํŠธ๋ฅผ ํ†ตํ•ด ์ž…๋ ฅํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์ˆ˜์ง‘ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” ํƒœ๊ทธ์ž…๋‹ˆ๋‹ค. input, button, textarea, select, checkbox, radio button, submit button ๋“ฑ์˜ ์ž…๋ ฅ ์–‘์‹ ํƒœ๊ทธ๋ฅผ ํ†ตํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ˆ˜์ง‘ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ˆ˜์ง‘ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ํ•˜๋‚˜๋กœ ๋ฌถ์–ด์„œ ์„œ๋ฒ„๋กœ ์ „์†ก์„ ํ•˜๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. 

<!DOCTYPE html>
<html>
  <body>
    <form action="http://jsonplaceholder.typicode.com/posts" method="get">
      userID: <input type="text" name="userID" value="1"><br>
      id: <input type="text" name="id" value="2"><br>
      <input type="submit" value="Submit">
    </form>
  </body>
</html>
form ํƒœ๊ทธ ์‹ค์ œ ๋ธŒ๋ผ์šฐ์ € ํ™”๋ฉด




submit ๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ form ์•ˆ์— ๋ฐ์ดํ„ฐ๋ฅผ ์„œ๋ฒ„๋กœ ๋ณด๋‚ธ ํ›„ ๋ฐ›์€ ๊ฒฐ๊ณผ

 ์œ„์— ์˜ˆ์‹œ์ฒ˜๋Ÿผ form ํƒœ๊ทธ๋Š” formํƒœ๊ทธ ์•ˆ์— ์žˆ๋Š” ์ž…๋ ฅ ์–‘์‹ ํƒœ๊ทธ๋“ค์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ชจ์•„์„œ ์„œ๋ฒ„์—์„œ ๋ณด๋‚ด๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. 
์•„๋ž˜ ์‚ฌ์ง„์€ ์šฐ๋ฆฌ๊ฐ€ ์‹ค์ƒํ™œ์—์„œ formํƒœ๊ทธ๋ฅผ ์ ‘ํ•  ์ˆ˜ ์žˆ๋Š” ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค.

form ํƒœ๊ทธ๋ฅผ ์ด์šฉํ•ด ๋งŒ๋“ค์–ด์ง„ ๋„ค์ด๋ฒ„ ๋กœ๊ทธ์ธ ์ž…๋ ฅ์ฐฝ

 

 

๐ŸŸก 1.2 form ํƒœ๊ทธ ์†์„ฑ

์†์„ฑ ์„ค๋ช… ์˜ˆ์‹œ
name form ํƒœ๊ทธ์˜ ์ด๋ฆ„์„ ์„ค์ •ํ•˜๋Š” ์†์„ฑ์ž…๋‹ˆ๋‹ค. <form name="login"></form>
action form ํƒœ๊ทธ ์•ˆ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ „์†กํ•  URL์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. <form action="http://jsonplaceholder.typicode.com/posts"></form>
method form ํƒœ๊ทธ ์•ˆ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์–ด๋– ํ•œ ๋ฐฉ์‹์œผ๋กœ ์ „์†กํ• ์ง€ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.

- GET : url ์•ˆ์— ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ด์•„์„œ ์„œ๋ฒ„๋กœ ์ „์†ก
- POST : Request Body์•ˆ์— ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ด์•„์„œ ์„œ๋ฒ„๋กœ ์ „์†ก
<form method="get"></form>
<form method="post></form>

 

๐ŸŸก 1.3 GET & POST

 - GET๊ณผ POST๋Š” HTTP request method๋ผ ํ•˜๊ณ  , HTTP ํ”„๋กœํ† ์ฝœ์„ ์ด์šฉํ•ด์„œ ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์„œ๋ฒ„์— ์ „๋‹ฌํ•˜๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค.

 ๐ŸŸฃ GET ๋ฐฉ์‹ 

  - GET ๋ฐฉ์‹์€ ์ „์†กํ•  URL ๋’ท๋ถ€๋ถ„์— ๋ฐ์ดํ„ฐ๋ฅผ ์ฟผ๋ฆฌ์ŠคํŠธ๋ง์œผ๋กœ ๋ณด๋‚ด๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค.

๋”๋ณด๊ธฐ

โ“ ์ฟผ๋ฆฌ์ŠคํŠธ๋ง์ด๋ž€?
https://www.website.com?querystring์—์„œ ?๋’ค์— ๋‚˜์˜ค๋Š” ๋ถ€๋ถ„์ด ์ฟผ๋ฆฌ์ŠคํŠธ๋ง์ž…๋‹ˆ๋‹ค. 

form ํƒœ๊ทธ ์•ˆ์— name = "bigone"์ด๋ผ๋Š” ๋ฐ์ดํ„ฐ์™€ like="kimchi"๋ผ๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ๋Š”, ์ด๊ฒƒ์„ ์„œ๋ฒ„๋กœ ๋ณด๋‚ผ ๋•Œ name๊ณผ like๋ฅผ url ๋’ค์— ๋ถ™์—ฌ์„œ ๋ณด๋‚ด๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.
https://www.website.com?name=bigone&like=kimchi์˜ ํ˜•ํƒœ๋กœ ์„œ๋ฒ„๋กœ ๋ณด๋‚ด์ง€๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด๋•Œ ์ฟผ๋ฆฌ์ŠคํŠธ๋ง์€ key=valueํ˜•ํƒœ๋กœ ๋งŒ๋“ค์–ด์ง€๋Š”๋ฐ, name๊ณผ like๊ฐ€ key๊ฐ€ ๋˜๊ณ  bigone๊ณผ kimchi๊ฐ€ value๋กœ ๋˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

๋ฐ์ดํ„ฐ๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ์ธ ๊ฒฝ์šฐ์—๋Š” &๋ฅผ ์ด์šฉํ•ด์„œ ์—ฐ๊ฒฐํ•ฉ๋‹ˆ๋‹ค.

ex) https://www.website.com?key1=value1&key2=value2&key3=value3

- URL์— ๋ฐ์ดํ„ฐ๊ฐ€ ๋ชจ๋‘ ๋‹ด๊ฒจ์„œ ๋…ธ์ถœ๋œ ์ƒํƒœ๋กœ ์ „์†ก์ด ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋ณด์•ˆ์— ๋ฌธ์ œ๊ฐ€ ์žˆ๊ณ , ๋ฐ์ดํ„ฐ์˜ ํ•œ๊ณ„๋„ ์žˆ์Šต๋‹ˆ๋‹ค(์ตœ๋Œ€ 255์ž)

- REST API์—์„œ GET ๋ฉ”์„œ๋“œ๋Š” ๋ชจ๋“  ๋˜๋Š” ํŠน์ • ๋ฆฌ์†Œ์Šค์˜ ์กฐํšŒ๋ฅผ ์š”์ฒญํ•ฉ๋‹ˆ๋‹ค.

 

GET ๋ฐฉ์‹ ์˜ˆ์‹œ(๊ฐ™์€ ์ƒ‰๋ผ๋ฆฌ key&amp;amp;amp;value)

 

<form action="https://www.website.com" method="get"></form>

 

 

๐ŸŸฃ POST ๋ฐฉ์‹ 

- POST ๋ฐฉ์‹์€ Request Body์— ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ด์•„์„œ ์„œ๋ฒ„๋กœ ์ „์†กํ•˜๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค.

- URL์— ๋ฐ์ดํ„ฐ๊ฐ€ ๋…ธ์ถœ์ด ๋˜์ง€ ์•Š์•„ GET๋ฐฉ์‹๋ณด๋‹ค ์•ˆ์ „ํ•˜์ง€๋งŒ, ์†๋„๋Š” GET๋ณด๋‹ค ๋Š๋ฆฝ๋‹ˆ๋‹ค.

- REST API์—์„œ POST ๋ฉ”์„œ๋“œ๋Š” ํŠน์ • ๋ฆฌ์†Œ์Šค์˜ ์ƒ์„ฑ์„ ์š”์ฒญํ•ฉ๋‹ˆ๋‹ค.

 

form ํƒœ๊ทธ ํ˜•ํƒœ postํ˜•์‹์œผ๋กœ ์ „์†กํ•œ ๋ฐ์ดํ„ฐ

<form action="https://www.website.com" method="post"></form>

 

 

๐Ÿ“ ์ •๋ฆฌ

  •  form ํƒœ๊ทธ๋Š” ์‚ฌ์šฉ์ž์—๊ฒŒ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅ๋ฐ›์•„์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์„œ๋ฒ„๋กœ ๋ณด๋‚ด์ฃผ๋Š” ์—ญํ• 
  •  form ํƒœ๊ทธ์—๋Š” name, action, method๋ผ๋Š” ์†์„ฑ์ด ์žˆ๋‹ค.
  •  method์—๋Š” GET๊ณผ POST๊ฐ€ ์žˆ๋Š”๋ฐ, GET์€ url๋’ค์— ๋ฐ์ดํ„ฐ๊ฐ€ ๋ถ™๋Š” ๊ฒƒ์ด๊ณ  POST๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ˆจ๊ฒจ์„œ ์„œ๋ฒ„๋กœ ๋ณด๋‚ด๋Š” ๋ฐฉ์‹.

 

๊ธด ๊ธ€ ์ฝ์–ด์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค :) 
ํ‹€๋ฆฐ ๋‚ด์šฉ์ด ์žˆ๊ฑฐ๋‚˜, ๋ง๋ถ™์ผ ๋‚ด์šฉ์ด ์žˆ๋‹ค๋ฉด ์–ธ์ œ๋“ ์ง€ ๋Œ“๊ธ€ ๋‹ฌ์•„์ฃผ์„ธ์š”!
์ œ ๊ธ€์ด ์กฐ๊ธˆ์ด๋‚˜๋งˆ ์ฝ์€ ์‹  ๋ถ„๋“ค์—๊ฒŒ ๋„์›€์ด ๋˜๋„๋ก ๋…ธ๋ ฅํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค
๋‹ค์Œ ํŽธ์— ๋ดฌ์š”~

728x90
๋ฐ˜์‘ํ˜•