Post datatable data to server in JSON format with column names?
Post datatable data to server in JSON format with column names?
proecil
Posts: 3Questions: 1Answers: 0
Hi there,
I'm trying to post datatable data to server through ajax call like this:
//Ajax call function
function formAjaxCall(form, dataTable){
var form = $(form);
var data = form.serializeArray();
if(dataTable) data.push({'name': 'dataTable', 'value': dataTable.fnGetData()});
$.ajax({
type: form.attr('method'),
url: form.attr('action'),
data: { data: data },
dataType: 'json',
success: function(response){
alert(response.message);
}
});
}
//Form and datatable sent to server:
data[0][name]:some_textField
data[0][value]:some_value
data[1][name]:dataTable
data[1][value][0][]:18
data[1][value][0][]:Jordan boots
data[1][value][0][]:4716659293521
data[1][value][0][]:Nike
data[2][value][0][]:15
data[2][value][0][]:Computer speakers
data[2][value][0][]:4736669294820
data[2][value][0][]:Logitech
It's OK, but I need to send this data in a JSON format including column names to catch them in a php function:
['datatable': {'id': 18, 'name': 'Jordan boots', 'ean': '4716659293521', 'brand': 'Nike'},
{'id': 15, 'name': ..........}];
Any help will be appreciated.
Thank you in advanced :)
This discussion has been closed.
Answers
Have a look at the
ajax.data
documentation. It has an example showing how to send data in a JSON format. To send column names, define the names usingcolumns.name
.Allan
Thank you so much Allan.
how can I trigger datatable ajax call to save data?
This is actually my code:
HTML
JS
Is it possible to send data to server on custom javascript ajax call?
Sorry for my rookie questions :/
You have input elements in the table? DataTables won't submit those values - you would need to do that yourself if that is what you wanted since DataTables doesn't know anything about them. Have a look at this example.
Allan
Hello again Allan,
Exactly, my table hasn't input elements. This is my table format:
Just want send data to server in json format with column names as index:
Thank you so much Allan.
Has or hasn't? Your HTML doesn't have input elements.
If you want to get the data form the table use
data()
and you can then send that to the server.columns.data
can be used to create JSON objects from an HTML table data source.Allan