JavaScript submit form excluding fields
I have a simple html form:
<form action="test" method="post" id="myForm">
<input type="text" name="myTextField">
<input type="text" name="myTextField2">
<input type="text" name="dontSubmitThisField">
</form>
And I need to send it using JavaScript, but I want to exclude the field dontSubmitThisField
from the request. Is there a way to do this without Ajax?
source to share
Just turn off the field.
Or do it via HTML if the field cannot be updated by the user: http://www.w3schools.com/tags/att_input_disabled.asp
Or do it via jQuery with a submit event:
$('#myForm').submit(function(){
$('input[name="dontSubmitThisField"]').prop('disabled', true);
});
source to share
You can disable the field and it won't be included in the post vars. Disabled form fields do not submit data
source to share
Disabled fields or fields without an attribute name
will not be sent.
However, if you want to name your fields and don't want to disable them, you can intercept all forms and disable all fields with the attribute data-no-submit
.
document.addEventListener('submit', function (e) {
if (!e.defaultPrevented) {
[].forEach.call(e.target.querySelectorAll('[data-no-submit]'), function (field) {
field.disabled = true;
});
}
});
Then you can simply do:
<input type="text" name="dontSubmitThisField" data-no-submit>
source to share