I want to send a form by clicking the Submit
button. The form originally only consists of a CSRF
token. When the user clicks submit, I want to fill up the form using vanilla JS with data gathered on the page.
Now here’s the HTML with the form and an input field which shall be injected afterwards:
<input id="user-input" type="text" name="userInput">
<form id="best-form-ever" method="post" action="/amaze-me">
<input type="hidden" name="_csrf" value="xyz1234">
<button id="submit-form" name="submit-form">Submit</button>
</form>
I have the following JavaScript code so far in place:
document.querySelector("#best-form-ever").addEventListener("submit", this.handleSubmit, false);
handleSubmit(event) {
event.preventDefault();
const form = document.getElementById("best-form-ever");
const userInput = document.querySelector("#user-input").innerHTML;
const payload = new FormData(form);
payload.append("userInput", userInput);
payload.submit(); /* this doesn't work :( */
}
Unfortunately, the form won’t get send this way since FormData:submit
is not a function.
So here’s my question:
How can I inject additional data to a form after the submit event?
3
Answers
try like this
If you set the attribute form on the input element and set the value to the ID of the form, it will be included in the form.
Elements can also be added on the submit event either by setting the
form
attribute on input elements or by adding elements directly to the form that is submitted:The main issue is that the
submit
function doesn’t exactly behave the same as clicking the submit button. From the docs:However, it looks like
requestSubmit
does not have such limitations, and will behave exactly the same as clicking submit. Note that this method is run against the Form DOM object, not FormData.An easier approach is to manually perform the http request and pass in the FormData object. More documentation on this can be found here: https://developer.mozilla.org/en-US/docs/Learn/Forms/Sending_forms_through_JavaScript#associating_a_formdata_object_and_a_form