Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save davidystephenson/9bab4617f1a4c8cbd66bad3a6631eb8d to your computer and use it in GitHub Desktop.

Select an option

Save davidystephenson/9bab4617f1a4c8cbd66bad3a6631eb8d to your computer and use it in GitHub Desktop.
const form = document.getElementById('productForm')
form.addEventListener('submit', async (event) => {
event.preventDefault()
const name = document.getElementById('pname')
const price = document.getElementById('price')
const units = document.getElementById('units')
const description = document.getElementById('desc')
const isAvailable = document.getElementById('instock')
const category = document.getElementById('category')
const image = document.getElementById('prodimg')
const data = {
name: name.value,
price: price.value,
quantity: units.value,
description: description.value,
isAvailable: isAvailable.checked
? 'Yes'
: 'No',
category: category.value,
image: image.value
}
const body = JSON.stringify(data)
const init = {
method: 'POST',
body
}
await fetch('http://localhost:3000/products', init)
form.reset()
window.location.href = './list_products.html'
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment