Just like the Caique commented, you can, with Javascript, add the property disabled at the time the form is submitted, so the browser will not include the blank fields:
const form = document.getElementById("form");
form.addEventListener("submit", function(event) {
for (let input of this.elements) {
if (!input.value) {
input.setAttribute("disabled", true);
}
}
});
The same happens if you define the property name for nil:
form.addEventListener("submit", function(event) {
for (let input of this.elements) {
if (!input.value) {
input.setAttribute("name", "");
}
}
});
Comments on the code
Through the document.getElementById we managed to obtain the reference in JS of the form in the DOM, ie, form will be the form we wish to work - realize that for this it will be necessary to define the attribute id no. After, using addEventListener, add a function that will always be run when the event submit form is triggered, that is, when the form is submitted - actually the event submit is executed immediately prior to the actual submission of the form by the browser. In this function, then, we go through all the elements (fields) of the form in question through a loop of repetition for, iterating on this.elements. The this, in this case, refers to the form. Thus, input will be the reference to each field of the form, then we check whether the respective value input.value is valid and when it is not, we define the property disabled as true of it. A field with the property disabled defined is ignored by the browser when the form is submitted, for this reason blank fields are not sent.
Anderson, worked friend very thank you. If it is not asking too much, could you explain to me what you did there? because I had previously tried something similar to jquery and was unsuccessful.
– Leandro Silva Campos
@Leandrosilvacampos I tried to explain, see if it was clear enough.
– Woss