Form submit with query params
up vote
1
down vote
favorite
I have a simple html form where I am trying to do a GET. I have few parameters in there that serve as query strings on submit. Here is the codepen: form example
When I submit the form, the action url becomeshttps://www.foo.com/?name=abc&age=20&homepage=http%3A%2F%2Flocalhost%3A8080%2Fhomepage.
When I don't give value for any one of the parameters (ex:name) in the form and submit, the url does not exclude the empty values and instead generates &name=&age=20.
Please advise if there is a way to have the action url not take the empty values on submit.
<form name="employeeForm" id="theForm" action="https://www.foo.com" method="get">
<div>
<table>
<tr>
<td><b> Name: </b></td>
<td><input name="name" id="name" value="abc" /><br/><br/></td>
</tr>
<tr>
<td><b> Age: </b></td>
<td><input name="age" id="age" value="20" /><br/><br/></td>
</tr>
<tr>
<td><b> Homepage: </b></td>
<td><input name="homepage" id="homepage" value="http://localhost:8080/homepage" /><br/><br/></td>
</tr>
</table><br/>
<button>Submit</button>
</div>
javascript html forms
add a comment |
up vote
1
down vote
favorite
I have a simple html form where I am trying to do a GET. I have few parameters in there that serve as query strings on submit. Here is the codepen: form example
When I submit the form, the action url becomeshttps://www.foo.com/?name=abc&age=20&homepage=http%3A%2F%2Flocalhost%3A8080%2Fhomepage.
When I don't give value for any one of the parameters (ex:name) in the form and submit, the url does not exclude the empty values and instead generates &name=&age=20.
Please advise if there is a way to have the action url not take the empty values on submit.
<form name="employeeForm" id="theForm" action="https://www.foo.com" method="get">
<div>
<table>
<tr>
<td><b> Name: </b></td>
<td><input name="name" id="name" value="abc" /><br/><br/></td>
</tr>
<tr>
<td><b> Age: </b></td>
<td><input name="age" id="age" value="20" /><br/><br/></td>
</tr>
<tr>
<td><b> Homepage: </b></td>
<td><input name="homepage" id="homepage" value="http://localhost:8080/homepage" /><br/><br/></td>
</tr>
</table><br/>
<button>Submit</button>
</div>
javascript html forms
Just handle it on the server as you are supposed to or ajax and remove empty fields
– mplungjan
Nov 8 at 9:22
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have a simple html form where I am trying to do a GET. I have few parameters in there that serve as query strings on submit. Here is the codepen: form example
When I submit the form, the action url becomeshttps://www.foo.com/?name=abc&age=20&homepage=http%3A%2F%2Flocalhost%3A8080%2Fhomepage.
When I don't give value for any one of the parameters (ex:name) in the form and submit, the url does not exclude the empty values and instead generates &name=&age=20.
Please advise if there is a way to have the action url not take the empty values on submit.
<form name="employeeForm" id="theForm" action="https://www.foo.com" method="get">
<div>
<table>
<tr>
<td><b> Name: </b></td>
<td><input name="name" id="name" value="abc" /><br/><br/></td>
</tr>
<tr>
<td><b> Age: </b></td>
<td><input name="age" id="age" value="20" /><br/><br/></td>
</tr>
<tr>
<td><b> Homepage: </b></td>
<td><input name="homepage" id="homepage" value="http://localhost:8080/homepage" /><br/><br/></td>
</tr>
</table><br/>
<button>Submit</button>
</div>
javascript html forms
I have a simple html form where I am trying to do a GET. I have few parameters in there that serve as query strings on submit. Here is the codepen: form example
When I submit the form, the action url becomeshttps://www.foo.com/?name=abc&age=20&homepage=http%3A%2F%2Flocalhost%3A8080%2Fhomepage.
When I don't give value for any one of the parameters (ex:name) in the form and submit, the url does not exclude the empty values and instead generates &name=&age=20.
Please advise if there is a way to have the action url not take the empty values on submit.
<form name="employeeForm" id="theForm" action="https://www.foo.com" method="get">
<div>
<table>
<tr>
<td><b> Name: </b></td>
<td><input name="name" id="name" value="abc" /><br/><br/></td>
</tr>
<tr>
<td><b> Age: </b></td>
<td><input name="age" id="age" value="20" /><br/><br/></td>
</tr>
<tr>
<td><b> Homepage: </b></td>
<td><input name="homepage" id="homepage" value="http://localhost:8080/homepage" /><br/><br/></td>
</tr>
</table><br/>
<button>Submit</button>
</div>
javascript html forms
javascript html forms
asked Nov 8 at 9:15
Mustang
287520
287520
Just handle it on the server as you are supposed to or ajax and remove empty fields
– mplungjan
Nov 8 at 9:22
add a comment |
Just handle it on the server as you are supposed to or ajax and remove empty fields
– mplungjan
Nov 8 at 9:22
Just handle it on the server as you are supposed to or ajax and remove empty fields
– mplungjan
Nov 8 at 9:22
Just handle it on the server as you are supposed to or ajax and remove empty fields
– mplungjan
Nov 8 at 9:22
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
It seems to me that we cannot do that with plain HTML. But it is quite easy with javascript.
This page shares a snippet which is pretty useful:
(It uses jQuery but you can easily adapt it with plain javascript)
jQuery(document).ready(function($){
// Remove empty fields from GET forms
$("form").submit(function() {
$(this).find(":input").filter(function(){ return !this.value; }).attr("disabled", "disabled");
return true; // ensure form still submits
});
// Un-disable form fields when page loads, in case they click back after submission
$( "form" ).find( ":input" ).prop( "disabled", false );
}
New contributor
mistiru is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
No problem - please do not QUOTE the code, just indent 4 spaces
– mplungjan
Nov 8 at 10:35
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
It seems to me that we cannot do that with plain HTML. But it is quite easy with javascript.
This page shares a snippet which is pretty useful:
(It uses jQuery but you can easily adapt it with plain javascript)
jQuery(document).ready(function($){
// Remove empty fields from GET forms
$("form").submit(function() {
$(this).find(":input").filter(function(){ return !this.value; }).attr("disabled", "disabled");
return true; // ensure form still submits
});
// Un-disable form fields when page loads, in case they click back after submission
$( "form" ).find( ":input" ).prop( "disabled", false );
}
New contributor
mistiru is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
No problem - please do not QUOTE the code, just indent 4 spaces
– mplungjan
Nov 8 at 10:35
add a comment |
up vote
1
down vote
It seems to me that we cannot do that with plain HTML. But it is quite easy with javascript.
This page shares a snippet which is pretty useful:
(It uses jQuery but you can easily adapt it with plain javascript)
jQuery(document).ready(function($){
// Remove empty fields from GET forms
$("form").submit(function() {
$(this).find(":input").filter(function(){ return !this.value; }).attr("disabled", "disabled");
return true; // ensure form still submits
});
// Un-disable form fields when page loads, in case they click back after submission
$( "form" ).find( ":input" ).prop( "disabled", false );
}
New contributor
mistiru is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
No problem - please do not QUOTE the code, just indent 4 spaces
– mplungjan
Nov 8 at 10:35
add a comment |
up vote
1
down vote
up vote
1
down vote
It seems to me that we cannot do that with plain HTML. But it is quite easy with javascript.
This page shares a snippet which is pretty useful:
(It uses jQuery but you can easily adapt it with plain javascript)
jQuery(document).ready(function($){
// Remove empty fields from GET forms
$("form").submit(function() {
$(this).find(":input").filter(function(){ return !this.value; }).attr("disabled", "disabled");
return true; // ensure form still submits
});
// Un-disable form fields when page loads, in case they click back after submission
$( "form" ).find( ":input" ).prop( "disabled", false );
}
New contributor
mistiru is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
It seems to me that we cannot do that with plain HTML. But it is quite easy with javascript.
This page shares a snippet which is pretty useful:
(It uses jQuery but you can easily adapt it with plain javascript)
jQuery(document).ready(function($){
// Remove empty fields from GET forms
$("form").submit(function() {
$(this).find(":input").filter(function(){ return !this.value; }).attr("disabled", "disabled");
return true; // ensure form still submits
});
// Un-disable form fields when page loads, in case they click back after submission
$( "form" ).find( ":input" ).prop( "disabled", false );
}
New contributor
mistiru is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited Nov 8 at 10:36
New contributor
mistiru is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
answered Nov 8 at 9:23
mistiru
1538
1538
New contributor
mistiru is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
mistiru is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
mistiru is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
No problem - please do not QUOTE the code, just indent 4 spaces
– mplungjan
Nov 8 at 10:35
add a comment |
1
No problem - please do not QUOTE the code, just indent 4 spaces
– mplungjan
Nov 8 at 10:35
1
1
No problem - please do not QUOTE the code, just indent 4 spaces
– mplungjan
Nov 8 at 10:35
No problem - please do not QUOTE the code, just indent 4 spaces
– mplungjan
Nov 8 at 10:35
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53204622%2fform-submit-with-query-params%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Just handle it on the server as you are supposed to or ajax and remove empty fields
– mplungjan
Nov 8 at 9:22