Adding POST parameters before submit
To add that using Jquery:
$('#commentForm').submit(function(){ //listen for submit event
$.each(params, function(i,param){
$('<input />').attr('type', 'hidden')
.attr('name', param.name)
.attr('value', param.value)
.appendTo('#commentForm');
});
return true;
});
Adding POST parameters after form submit
Have <input type="hidden" ...>
elements inside the form.
Assign the values to those elements.
jQuery - add additional parameters on submit (NOT ajax)
This one did it for me:
var input = $("<input>")
.attr("type", "hidden")
.attr("name", "mydata").val("bla");
$('#form1').append(input);
is based on the Daff's answer, but added the NAME attribute to let it show in the form collection and changed VALUE to VAL
Also checked the ID of the FORM (form1 in my case)
used the Firefox firebug to check whether the element was inserted.
Hidden elements do get posted back in the form collection, only read-only fields are discarded.
Michel
How to add additional fields to form before submit?
Yes.You can try with some hidden params.
$("#form").submit( function(eventObj) {
$("<input />").attr("type", "hidden")
.attr("name", "something")
.attr("value", "something")
.appendTo("#form");
return true;
});
Adding additional parameter to form submit
Appending query string parameters onto the form action, and trying to change that at runtime is tricky (but not impossible). Far easier is to use hidden fields:
<form method="post" action="/Lot/LotList" id="filterForm">
<input type="hidden" name="auctionEventId" value="@auctionEventId" />
...
So now all you have to do is add another hidden field for "showAll"
<form method="post" action="/Lot/LotList" id="filterForm">
<input type="hidden" name="auctionEventId" value="@auctionEventId" />
<input type="hidden" name="showAll" value="false" id="showAllField" />
...
And just hook up a jquery event on your showAll button:
<input id="showAllButton" type="button"/>
jQuery:
$('#showAllButton').click(function(){
$('#showAllField').val("true");
$('#filterForm').submit();
});
Change form POST data before submit with jQuery not working
Try something like this.
You will need to use
preventDefault
to make any changes.
$("#form_id").on("submit", function (e) {
e.preventDefault();//stop submit event
var self = $(this);//this form
$("#change_value").val("deneme");//change input
$("#form_id").off("submit");//need form submit event off.
self.unbind('submit');
self.submit();//submit form
});
Changing post parameters of form on submit without it being visible to the user
Add a hidden input field to the form. In the OnSubmit event handler, set that field to whatever value you want. Have the backend ignore the field that's visible to the user and only use the hidden one (you can accomplish that via how you name the fields).
JavaScript post request like a form submit
Dynamically create <input>
s in a form and submit it
/**
* sends a request to the specified url from a form. this will change the window location.
* @param {string} path the path to send the post request to
* @param {object} params the parameters to add to the url
* @param {string} [method=post] the method to use on the form
*/
function post(path, params, method='post') {
// The rest of this code assumes you are not using a library.
// It can be made less verbose if you use one.
const form = document.createElement('form');
form.method = method;
form.action = path;
for (const key in params) {
if (params.hasOwnProperty(key)) {
const hiddenField = document.createElement('input');
hiddenField.type = 'hidden';
hiddenField.name = key;
hiddenField.value = params[key];
form.appendChild(hiddenField);
}
}
document.body.appendChild(form);
form.submit();
}
Example:
post('/contact/', {name: 'Johnny Bravo'});
EDIT: Since this has gotten upvoted so much, I'm guessing people will be copy-pasting this a lot. So I added the hasOwnProperty
check to fix any inadvertent bugs.
Related Topics
How to Load Image Files with Webpack File-Loader
React.Js Replace Img Src Onerror
How to Scroll the Window Using Jquery $.Scrollto() Function
Why JavaScript Functions Always Return a Value
JavaScript Equivalent of Jquery's Extend Method
How to Make an Angularjs Directive to Stoppropagation
Multiple Conditions for JavaScript .Includes() Method
Angular App Has to Clear Cache After New Deployment
How to Access Object Property with Invalid Characters
How to Join Two JavaScript Objects, Without Using Jquery
How to Block Users from Closing a Window in JavaScript
Ajax Post and Plus Sign ( + ) -- How to Encode
How to Detect Timeout on an Ajax (Xmlhttprequest) Call in the Browser
Jquery: How to Get the Event Object in an Event Handler Function Without Passing It as an Argument
Referenceerror: Document Is Not Defined (In Plain JavaScript)