๐Ÿšฆ HTML5์˜ ๋‹ค๋ฅธ ์ž‘์—…์— ์–‘์‹ ์ œ์ถœ

9991 ๋‹จ์–ด formhtmlfrontendjavascript
์˜ค๋žซ๋™์•ˆ ์ €๋Š” ํ•˜๋‚˜์˜ Form์ด JS ์—†์ด ํ•˜๋‚˜์˜ Action URL์—๋งŒ ์ œ์ถœ๋  ์ˆ˜ ์žˆ๋‹ค๊ณ  ๋ฏฟ์—ˆ์Šต๋‹ˆ๋‹ค.

I couldnโ€™t be more wrong.



ํ•˜์ง€๋งŒ JavaScript ๋ฐ AJAX/Fetch๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ณผ๊ฑฐ์˜ ๋ฐฉ์‹์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.


๋ฌธ์ œ



์‚ฌ์šฉ์ž ํ”„๋กœํ•„์— ์ฃผ์†Œ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜ ์—…๋ฐ์ดํŠธํ•˜๋ ค๋Š” HTML ์–‘์‹์„ ๋”ฐ๋ฅด๋Š” ๊ฒƒ์„ ๊ณ ๋ คํ•˜์‹ญ์‹œ์˜ค.

<form id='addressForm'>
  ...
  <!-- Form Fields -->
  ...
  <button id='addAddress' class="btn btn-primary">Add Address</button>
  <button id='updateAddress' class="btn btn-success">Update Address</button>
</form>

์œ„์˜ ์–‘์‹์—๋Š” Add Address ๋˜๋Š” Update Address์— ๋Œ€ํ•œ ์ œ์ถœ ๋ฒ„ํŠผ์ด ์žˆ์ง€๋งŒ POST ์š”์ฒญ์€ /addAddress์—๋งŒ ๋ณด๋ƒ…๋‹ˆ๋‹ค.

์ฆ‰, ์—…๋ฐ์ดํŠธ ์ฃผ์†Œ๋Š” ํ˜„์žฌ ์ฃผ์†Œ๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๋Š” ๋Œ€์‹  ์ƒˆ ์ฃผ์†Œ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

์ด ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋ ค๋ฉด JavaScript๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์ด์ œ JavaScript์—๋Š” ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค. ํ•œ ๊ฐ€์ง€ ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์€ AJAX ์š”์ฒญ์„ ํ†ตํ•ด ์–‘์‹์„ ์ œ์ถœํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

AJAX๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์œ„์˜ ์–‘์‹์„ ๋‹ค๋ฅธ ์ž‘์—… ๋์ ์— ์ œ์ถœํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

document.addEventListener('DOMContentLoaded', () => {
  const form = document.querySelector('#addressForm')
  const addButton = document.querySelector('#addAddress')
  const updateButton = document.querySelector('#updateAddress')

  addButton && addButton.addEventListener('click', (e) => {
    e.preventDefault()

    if (!form.checkValidity()) {
      return
    }

    fetch('/addAddress', { method: 'POST', body: new FormData(form) })
      .then((r => r.json()))
      .then(console.log)
  })

  updateButton && updateButton.addEventListener('click', (e) => {
    e.preventDefault()

    if (!form.checkValidity()) {
      return
    }

    fetch('/updateAddress', { method: 'POST', body: new FormData(form) })
      .then((r => r.json()))
      .then(console.log)
  })
})

์ด์ œ JavaScript๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด HTML๊ณผ ๋ณ€ํ˜• ์ œ์ถœ์ด ๋งค์šฐ ๋ฐ€์ ‘ํ•˜๊ฒŒ ๊ฒฐํ•ฉ๋˜์–ด ์žˆ์Œ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์–‘์‹์„ ์—ฌ๋Ÿฌ ๋์ ์œผ๋กœ ์ „์†กํ•˜๋Š” ์ด ์ด์ „ ๋ฐฉ์‹์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹คhttps://multi-action-forms.netlify.app/with-ajax.html.

Now what is the solution?


ํ•ด๊ฒฐ์ฑ…



์ œ์ถœ ๋ฒ„ํŠผ์— ๋Œ€ํ•œ ์ด๋Ÿฌํ•œ ์†์„ฑ์„ ๋ฐœ๊ฒฌํ–ˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์—์„œ ๋‹ค๋ฅธ HTTP ๋ฉ”์„œ๋“œ ๋ฐ ์ธ์ฝ”๋”ฉ ์œ ํ˜•์„ ์‚ฌ์šฉํ•˜์—ฌ ๋‹ค๋ฅธ URL์— ์ œ์ถœํ•  ์–‘์‹์„ ๊ตฌ์ฒด์ ์œผ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด๋Ÿฌํ•œ ์†์„ฑ์€ fromaction, formmethod ๋ฐ formenctype์ž…๋‹ˆ๋‹ค.

์ด๋Ÿฌํ•œ ์†์„ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ผ๋ฐ˜ ์ œ์ถœ ๋ฒ„ํŠผ๊ณผ ๊ธฐ๋ณธ ์†์„ฑ์„ ์–‘์‹์— ์ถ”๊ฐ€ํ•˜๋Š” ์ž‘์—…์œผ๋กœ ๋Œ์•„๊ฐˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด์™€ ํ•จ๊ป˜ ์ด์ „ ์ค„์—์„œ ์–ธ๊ธ‰ํ•œ ์†์„ฑ์„ ๊ฐ€์ง„ ๋‹ค๋ฅธ ์ œ์ถœ ๋ฒ„ํŠผ์„ ์ถ”๊ฐ€ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค.

์ƒˆ HTML ์–‘์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

<form action="/addAddress" method="POST">
  ...
  <!-- Form Fields -->
  ...
  <button type="submit" class="btn btn-primary">Add</button>
  <button type="submit" class="btn btn-success" formaction="/updateAddress" formmethod="POST">Update</button>
</form>

๋ฒ„ํŠผ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋ฒ„ํŠผ ์œ ํ˜•์˜ ์ž…๋ ฅ์—์„œ๋„ ์ด๋Ÿฌํ•œ ์†์„ฑ์„ ํ—ˆ์šฉํ•˜์—ฌ ์–‘์‹ ์ œ์ถœ์„ ๋‹ค๋ฅธ ๋์ ์œผ๋กœ ๋ณด๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด์ต


  • ๊ธฐ๋ณธ ๊ธฐ๋Šฅ์„ ์œ„ํ•ด JS๋ฅผ ์ฒจ๋ถ€ํ•  ํ•„์š”๊ฐ€ ์—†์Œ
  • HTML5 ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ API๋ฅผ ์ •์ƒ์ ์ธ ํ๋ฆ„์œผ๋กœ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์Œ
  • JS๊ฐ€ ๋น„ํ™œ์„ฑํ™”๋œ Pages์—์„œ ์–‘์‹์ด ์ž‘๋™ํ•  ์ˆ˜ ์žˆ์Œ(์˜ˆ์™ธ ์žˆ์Œ)

  • ์šฐ๋ ค ์‚ฌํ•ญ


  • ๋ธŒ๋ผ์šฐ์ € ์ง€์›, ์ด์ „ ๋ธŒ๋ผ์šฐ์ €๋Š” ์ด ๊ธฐ๋Šฅ๊ณผ ์ž‘๋™ํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ Polyfill/Shim์ด ํ•„์š”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค
  • .
  • ๋‹จ์ผ ํŽ˜์ด์ง€ ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ, MicroFrontends ๋“ฑ๊ณผ ๊ฐ™์€ ์ตœ์‹  ํ”„๋ŸฐํŠธ ์—”๋“œ ์•ฑ์€ ์ด ์ž‘์—…์„ ์œ„ํ•ด ์•ฝ๊ฐ„์˜ ์ถ”๊ฐ€ ์กฐ์ •์ด ํ•„์š”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค
  • .




    time2hack / ๋ฉ€ํ‹ฐ ์•ก์…˜ ์–‘์‹ ์˜ˆ์ œ






    Demo


    ๊ฒฐ๋ก 



    ์ ์€ ์ฝ”๋“œ๋กœ ๋” ๋งŽ์€ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด ๋ธŒ๋ผ์šฐ์ €์˜ ๋‚ด์žฅ API๋ฅผ ํ™œ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ดํŽด๋ณด์•˜์Šต๋‹ˆ๋‹ค.

    Would you use in you application? Why or Why not?



    ๋Œ“๊ธ€ ๐Ÿ’ฌ ๋˜๋Š” Twitter at ๋ฐ/๋˜๋Š”์„ ํ†ตํ•ด ์•Œ๋ ค์ฃผ์„ธ์š”.

    ์ด ๊ธ€์ด ๋„์›€์ด ๋˜์…จ๋‹ค๋ฉด ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค๊ณผ ๊ณต์œ ํ•ด์ฃผ์„ธ์š” ๐Ÿ—ฃ

    ๋ฐ›์€ ํŽธ์ง€ํ•จ์œผ๋กœ ๋ฐ”๋กœ ์ƒˆ ๊ฒŒ์‹œ๋ฌผ์„ ๋ฐ›์œผ๋ ค๋ฉด ๋ธ”๋กœ๊ทธ๋ฅผ ๊ตฌ๋…ํ•˜์‹ญ์‹œ์˜ค.


    ํ•™์ 


  • Iconfinder์˜ ์•„์ด์ฝ˜
  • ์‚ฌ์ง„ ์ œ๊ณต: Mitchell Luo on Unsplash



  • 2021๋…„ 1์›” 13์ผ https://time2hack.com์—์„œ ์›๋ž˜ ๊ฒŒ์‹œ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

    ์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ