'How do I exclude certain form fields upon submission of the form without disabling the field

If I have a form with the following 3 fields:

First Name Last Name Date Of Birth

When the user fills in all 3 fields and submits you will get the URL

http: //fakeURL.php?firstName=Fred&lastName=Flintstone&DateOfBirth=2/5/1952

However, if the user only fills in First Name and Date Of Birth you will get

http: //fakeURL.php?firstName=Fred&lastName=&DateOfBirth=2/5/1952 (where lastName has no value)

How do I achieve

http: //fakeURL.php?firstName=Fred&DateOfBirth=2/5/1952 (where lastName and value are removed from the URL)

I don't want to disable the input field upon using onsubmit. Is there a better way than disabled the input field?

Please help...



Solution 1:[1]

Just remove the "name" attribute from the input element.

// using jQuery
$('#inputId').removeAttr('name');

// native Javascript
document.getElementById('inputId').removeAttribute('name');

Solution 2:[2]

You must either:

  1. Remove or disable the field from the form before submitting it
  2. Don't submit the form, instead redirect to a URL you construct from the form yourself
  3. Make an AJAX request instead of leaving the page

Aside from those options, you can't submit this form via GET without all the inputs becoming part of the URL. That's part of the HTML and HTTP specifications.

Solution 3:[3]

if you are working on angular and using (ngModel), Remove the name attribute in the input field and add

[ngModelOptions]="{standalone: true}"

Solution 4:[4]

You should use forms with the GET method only when the new page is supposed to be bookmarked and passed around.

Since you are talking about you taking input from the user (and I presume you also store that input in a database or some similar permanent storage), you should be using POST instead.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Dan Grossman
Solution 3
Solution 4