0

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:

enter image description here

The serialized datatables from values looks like this (if you print_r($_POST) for debugging):

enter image description here

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...

1 Answer 1

1

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"
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry, I don't know how I missed that. the code was right, I forgot to return the response like you said. Good catch, thanks :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.