I have a jQuery ajax function and would like to submit an entire form as post data and will transfer some data parameter as well.
var serial = $("#formProjectBilling").serialize();
$.ajax({
url: base_url+"sales/Sales_invoice_form/saveSalesInvoice",
type: 'post',
data: serial + {
'orderItems': orderItems,
'orderTotal': totalamt,
'freight': freight,
'gen_disc':gen_disc,
'otp': otp,
'notes': $("#notes").val(),
'idno': $("#idno").val(),
'acctno': $("#hdnAcctNo").val(),
'itemlocid': $("#location_id").val(),
'shipping_id': $("#shipping_id").val(),
'sales_date': $("#sales_date").val(),
'discamt': $("#discount").text(),
'gendisctype': $("#gen_disc_type").val()
},
It only transfers the serialized form, but not the other data in parameters.
First you need to convert your object to url param query:
var str = "";
for (var key in yourData) {
if (str != "") {
str += "&";
}
str += key + "=" + yourData[key];
}
then into your ajax in the line where you pass data, just use the converted url:
...
data: serial + '&' + str;
...
Full Code:
var serial = $("#formProjectBilling").serialize();
// your data
var data = {
'orderItems': orderItems,
'orderTotal': totalamt,
'freight': freight,
'gen_disc': gen_disc,
'otp': otp,
'notes': $("#notes").val(),
'idno': $("#idno").val(),
'acctno': $("#hdnAcctNo").val(),
'itemlocid': $("#location_id").val(),
'shipping_id': $("#shipping_id").val(),
'sales_date': $("#sales_date").val(),
'discamt': $("#discount").text(),
'gendisctype': $("#gen_disc_type").val()
}
// conversion object to url params
var str = "";
for (var key in data) {
if (str != "") {
str += "&";
}
str += key + "=" + data[key];
}
$.ajax({
url: base_url + "sales/Sales_invoice_form/saveSalesInvoice",
type: 'post',
data: serial + '&' + str
})
Try this
var serial = $("#formProjectBilling").serializeArray();
$.ajax({
url: base_url+"sales/Sales_invoice_form/saveSalesInvoice",
type: 'post',
data: {...serial,...{
'orderItems': orderItems,
'orderTotal': totalamt,
'freight': freight,
'gen_disc':gen_disc,
'otp': otp,
'notes': $("#notes").val(),
'idno': $("#idno").val(),
'acctno': $("#hdnAcctNo").val(),
'itemlocid': $("#location_id").val(),
'shipping_id': $("#shipping_id").val(),
'sales_date': $("#sales_date").val(),
'discamt': $("#discount").text(),
'gendisctype': $("#gen_disc_type").val()
},
Jquery serialize()
function effectively turns the form values into a valid query string. Use $.param(object)
to send other params in some object variable
var values = [
{ 'orderItems': orderItems },
{ 'orderTotal': orderTotal },
{ 'freight': freight }
{ ... }
]
var data = form.serialize() + '&' + $.param(values);
Read more http://api.jquery.com/jQuery.param/