For example:
<input type="checkbox" name="fruits[]" value="apple">
<input type="checkbox" name="fruits[]" value="banana">
<input type="checkbox" name="fruits[]" value="orange">
Note that the name attribute is "fruits[]" which indicates that this is an array of values.
For example:
<form>
<input type="checkbox" name="fruits[]" value="apple">
<input type="checkbox" name="fruits[]" value="banana">
<input type="checkbox" name="fruits[]" value="orange">
<button type="submit">Submit</button>
</form>
const form = document.querySelector('form');
form.addEventListener('submit', (e) => {
e.preventDefault();
const formData = new FormData(form);
fetch('http://example.com', {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.log(error));
});
This code listens for the form's submit event and prevents the default behavior. It then creates a new FormData object from the form, which includes all the selected checkboxes. The fetch method is used to make a POST request to the server with the FormData object as the payload. Finally, the response from the server is logged to the console.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2022-09-13 11:00:00 +0000
Seen: 16 times
Last updated: Mar 11 '23
What is the process for generating a dynamic subdomain/URL using vue.js?
How can the style of the loader be modified while the form submission is being processed?
I'm attempting to develop a Javascript-based comments section for my website.
What are some feasible methods to enable MIDI file playback on a web browser?
What is the method to hide the scroll button when reaching the bottom?
What is the process of using a custom nunjucks filter to filter collections in an eleventy template?
What is the method to create a dynamic checkbox using API data?
How can the user id from the login object be utilized in the resolvers after logging in?
What steps can I take to prevent my webpage from slowing down when all parts of a div are displayed?