get form response with javascript

Solutions on MaxInterview for get form response with javascript by the best coders in the world

showing results for - "get form response with javascript"
Marco
24 Oct 2019
1document.forms['myFormId'].addEventListener('submit', (event) => {
2    event.preventDefault();
3    // TODO do something here to show user that form is being submitted
4    fetch(event.target.action, {
5        method: 'POST',
6        body: new URLSearchParams(new FormData(event.target)) // event.target is the form
7    }).then((resp) => {
8        return resp.json(); // or resp.text() or whatever the server sends
9    }).then((body) => {
10        // TODO handle body
11    }).catch((error) => {
12        // TODO handle error
13    });
14});
15