Reputation: 473
I am able to submit a form as Post
type using the following in my javascript:
$("#receiptsForm").submit();
But I also want send across a parameter myparam
as well with the request which I'll be retrieving in my spring controller using httpServletRequest.getParameter("myparam")
:
var myparam = "abc";
$("#receiptsForm").submit();
What's the best I can do?
Upvotes: 4
Views: 48823
Reputation: 9635
try this
function form_submit()
{
//var myparam = "abc";
// add hidden field to your form name="myparam" and value="abc"
$('#receiptsForm').append('<input type="hidden" name="myparam " value="abc" />');
$("#receiptsForm").submit();
}
Upvotes: 10
Reputation: 1121
Looks like you are using jquery..there are a couple of ways this can be done..either you can make it a object and then pass it as data in an ajaxrequest
var myparam = "abc";
var data_to_be_sent = {"myparam":myparam};
then in the data field of the ajax request, you can
data : data_to_be_sent.
Or you simply have a hidden field in the form and then on submit you can give it a value of myparam
Upvotes: 2
Reputation: 44834
There are two ways that spring to mind
a) include a hidden form field called myparam
in your form, and the use jquery to populate it with abc
before submitting.
b) use Jquery's ajax to call a ajax post
, and before doing this set the data parameter.
$.ajax({
type: "POST",
url: url,
data: data,
success: success,
dataType: dataType
});
Upvotes: 1
Reputation: 6525
Try this,
var input = $("<input>")
.attr("type", "hidden")
.attr("name", "mydata").val("bla");
$('#receiptsForm').append($(input));
$('#receiptsForm').submit();
Upvotes: 5
Reputation: 6181
Try using serializeArray
var data = $('#receiptsForm').serializeArray();
data.push({name: 'myparam', value: 'MyParamValue'});
You can send the data
like:
$.ajax({
...
data: data,
...
});
Upvotes: 3