Reputation: 24116
I have a datatables with some inputs in certain columns that are editable. I want to save the edited values in db.
This is my datatables:
The serialized datatables from values looks like this (if you print_r($_POST)
for debugging):
javascript - I post the datatables form via ajax request to my laravel api controller method like this:
// Serialize the datatable table into post string
var poDetailUpdates = create_po_details_table.$('input').serialize();
// Call po api to save changes
$.ajax({
type: "POST",
data: poDetailUpdates,
url: '/api/po/update-create-lines/',
complete: function(response) {
if (response.status === 200) {
alert(response.responseText); // success
} else {
alert(response.responseText); // error
}
}
});
php - this is how I handle my form post in laravel:
public function postUpdateCreateLines() {
DB::beginTransaction();
try {
foreach ($_POST as $column_name => $values) {
foreach ($values as $line_id => $new_value) {
DB::table('purchase_order_details')
->where('id', '=', $line_id)
->update(array($column_name => $new_value));
}
}
DB::commit();
Response::make('Purchase order details updated', 200);
} catch (Exception $ex) {
DB::rollback();
Response::make('Purchase order details not updated - '. $ex->getMessage(), 500);
}
}
When the code runs, I get an empty alert()
in javascript once the ajax request completes.
I thought the issue might be with the DB transaction related code, so I tried commenting out the following:
// DB::beginTransaction();
// DB::commit();
// DB::rollback();
But this also yields the same result (empty alert). Any idea what might be wrong? how to debug this? Something must be going wrong somewhere...
Upvotes: 0
Views: 1362
Reputation: 7371
Your postUpdateCreateLines()
method is not actually returning anything; you are making a response, but you need to return
it:
return Response::make('Purchase order details updated', 200);
However, there are some other improvements you could make. It is best to return a JSON response to your $.ajax
call, and then rely on jQuery's ajax .done
and .fail
handlers to deal with the response appropriately (rather than using complete
):
return Response::json(
['status' => 'success',
'msg' => 'Purchase order details updated']
, 200)
...and add the response dataType
to your ajax call:
dataType: "json"
Upvotes: 1