ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ


๋‹ค์Œ๊ณผ ๊ฐ™์ด form ํƒœ๊ทธ ๋‚ด์—์„œ Input value ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๊ธฐ ์œ„ํ•ด์„œ formData ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์‚ฌ์šฉํ•˜์˜€๋‹ค.

 

์ดํ›„ formData ์ธ์Šคํ„ด์Šค๋ฅผ ์ฝ”๋“œ ์ƒ์—์„œ ์ฐธ์กฐ ๋ฐ ์‚ฌ์šฉํ•˜๊ธฐ ์ด์ „์— ์ฝ˜์†” ์ฐฝ์—์„œ ์–ด๋–ค ์ •๋ณด๊ฐ€ ์ถœ๋ ฅ๋˜๋Š”์ง€ ํ™•์ธํ•˜๋ ค๊ณ  ํ–ˆ์œผ๋‚˜ formData์— ๋Œ€ํ•œ ์ง์ ‘์ ์ธ ๋ฐ์ดํ„ฐ ์ •๋ณด๊ฐ€ ์•„๋‹Œ FormData ๊ฐ์ฒด์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๋ฐ˜ํ™˜ํ•  ๋ฟ์ด์—ˆ๋‹ค.

 

FormData

 

formData ๊ฐ์ฒด

 

The FormData  interface provides a way to easily construct a set of key/value pairs representing form fields and their values, which can then be easily sent using the XMLHttpRequest.send() method. It uses the same format a form would use if the encoding type were set to "multipart/form-data".

 

MDN์— ๋”ฐ๋ฅด๋ฉด formData ๊ฐ์ฒด๋Š” ๋‹จ์ˆœํ•œ ๊ฐ์ฒด๊ฐ€ ์•„๋‹Œ XMLHttpRequest ์ „์†ก์„ ์œ„ํ•ด ์„ค๊ณ„๋œ ํŠน์ˆ˜ํ•œ ๊ฐ์ฒด ํ˜•ํƒœ์ด๋‹ค.

 

๋”ฐ๋ผ์„œ ๋ฌธ์ž์—ดํ™”ํ•  ์ˆ˜ ์—†๋Š” ๊ฐ์ฒด์ด๊ธฐ ๋•Œ๋ฌธ์— console.log๋ฅผ ํ†ตํ•ด ์ถœ๋ ฅํ•˜๊ฑฐ๋‚˜ ์ฝ”๋“œ ์ƒ์—์„œ ์ง์ ‘ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์—ˆ๋˜ ๊ฒƒ์ด๋‹ค.

 

ํ•ด๋‹น formData key์™€ value ์Œ์— ๊ด€๋ จ๋œ ์ •๋ณด๋ฅผ ์–ป๊ธฐ ์œ„ํ•ด์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

 

 1. formData.get() ๋ฉ”์„œ๋“œ

let info = [formData.get('contact'), formData.get('delivery')];

 

 2. formData.keys()/values() ๋ฉ”์„œ๋“œ

for (let key of formData.keys()) {
  console.log(key);
}

for (let value of formData.values()) {
  console.log(value);
}

 

 3. ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ ํ›„ Array.from()๋ฅผ ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ๋ฐฐ์—ดํ™”

const formRecords = Array.from(new FormData($formTag));

(...)

$currentItem.innerHTML = formRecords
    .map((record) => `${formMap[record[0]]}: ${record[1]}`) //key๋Š” ๋ฌธ์ž์—ด ํƒ€์ž…์œผ๋กœ ์ €์žฅ๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ฐฐ์—ด ๋ฆฌํ„ฐ๋Ÿด์„ ํ†ตํ•ด ํ”„๋กœํผํ‹ฐ ์ ‘๊ทผ
    .join('<br />');
    //๋ฐฐ์—ดํ™” ๋œ formData๋Š” ํ•ด์‰ฌ์™€ ๊ฐ™์ด key, value๋กœ ์ด๋ฃจ์–ด์ง„ ํ•œ ์Œ
    //formMap = { contact: ์—ฐ๋ฝ ์ˆ˜๋‹จ, delivery: ๋ฐฐ์†ก ์ˆ˜๋‹จ }
๋Œ“๊ธ€
๊ณต์ง€์‚ฌํ•ญ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€
Total
Today
Yesterday
๋งํฌ
TAG
more
ยซ   2025/01   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
๊ธ€ ๋ณด๊ด€ํ•จ