Form submit with query params











up vote
1
down vote

favorite
1












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>











share|improve this question






















  • Just handle it on the server as you are supposed to or ajax and remove empty fields
    – mplungjan
    Nov 8 at 9:22

















up vote
1
down vote

favorite
1












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>











share|improve this question






















  • Just handle it on the server as you are supposed to or ajax and remove empty fields
    – mplungjan
    Nov 8 at 9:22















up vote
1
down vote

favorite
1









up vote
1
down vote

favorite
1






1





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>











share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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




















  • 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














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 );

}





share|improve this answer










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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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
































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 );

}





share|improve this answer










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















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 );

}





share|improve this answer










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













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 );

}





share|improve this answer










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 );

}






share|improve this answer










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.









share|improve this answer



share|improve this answer








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














  • 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


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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




















































































Popular posts from this blog

How to pass form data using jquery Ajax to insert data in database?

National Museum of Racing and Hall of Fame

Guess what letter conforming each word