Hello!
I’m facing a strange behavior when sending forms. Currently i’m using Next.JS (which i deploy through Netlify as a static site) and i’m sending forms using fetch()
.
Here’s my code:
fetch('/', {
method: 'POST',
body: formData,
})
i’m getting formData
using the following method:
export function encodeNetlifyFormData(data) {
const formData = new FormData()
Object.keys(data).forEach(k => {
formData.append(k, data[k])
})
return formData
}
and to the <form />
tag i’ve just added the name
attribute.
Everything is working fine (the form is submitted and the data is saved and sended correctly), but the only issue i found here is that in the Netlify Dashboard (and also through the email notification), the values are saved/paired with the label key of the input field.
So for instance, if i have this code:
<div>
<label>
MY LABEL
<input type="email" name="email" />
</label>
</div>
i will see this:
MY LABEL: xxx@xxx.com
Is there a way to customize the label value? Because in case i use simple labels and input fieds there’s no issue at all, but in the case i use radio buttons, maybe with labels Yes
or No
, i get this result:
Yes: Yes
which is not useful at all.
The only way i found to customize
the labels is by sending the form data by using the following method:
const encode = data => {
return Object.keys(data)
.map(key => encodeURIComponent(key) + "=" + encodeURIComponent(data[key]))
.join("&")
}
but in case i want to upload a file (like a pdf
), the file isn’t being uploaded.
Is there a way to handle both cases?
Thanks!