laravel5.4 ajax,Laravel 5.4: In-Place Editing with Ajax

The page I currently have shows a table that dynamically generates rows showing user information. Each row has an Edit button that, when clicked, turns the respective cells into inputs, and turns the Edit button into a Save button. When that button's clicked, the input values for that user's row should be stored into the database.

I admittedly don't have a lot of experience with Ajax, but I've been looking online and I do believe I'm following the general procedure for calling a Controller function through an Ajax call properly. However, I'm still getting a 500 error when I try to test it. I believe it may be in how I am obtaining the request and sending back the response, but I'm unsure of the specific error.

My code contains as follows:

home.blade.php

....

@foreach($users as $user)

@endforeach

....

function save_row(num, user)

{

var id = 'row' + num;

$.ajax({

method: 'post',

url: '/update-table',

data: {

'_token': $('input[name=_token]').val(),

'first_name': $('input[id=first_name_input_' + id + ']').val(),

'last_name': $('input[id=last_name_input_' + id + ']').val(),

'user': user

},

success: function(response){

console.log("It worked!");

console.log(response);

},

error: function(jqXHR, textStatus, errorThrown) {

console.log("It failed!");

console.log(jqXHR);

console.log("AJAX error: " + textStatus + ' : ' + errorThrown);

}

});

}

HomeController.php

public function updateTable(Users $users){

$user = request()->input('employee');

$first_name = request()->input('first_name');

$last_name = request()->input('last_name');

$users->editUser($user, $first_name, $last_name);

return response()->json($user);

}

Users.php

public function editUser($user, $first_name, $last_name) {

$user->first_name = $first_name;

$user->last_name = $last_name;

$user->save();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值