0

Im using a toggle button switch:

<td><input data-id="{{$user->id}}" class="toggle-class" type="checkbox" data-onstyle="success" data-offstyle="danger" data-toggle="toggle" data-on="Active" data-off="Inactive" {{ $user->statusBol ? 'checked' : '' }}></td>
       

Here is my script with the ajax:

<script>
$(function(){
    $('.toggle-class').change(function(){
        var statusUser = $(this).prop('checked') == true ? 1 : 0; //1 active, 0 inactive
        var user_id = $(this).data('id');
            $.ajax({
                type: "GET",
                dataType : "json",
                url: 'changeUserStatus',
                data : {'statusUser': statusUser, 'user_id': user_id},
                success: function(data){
                    console.log('Success')
                }
            });
    });
    });
</script>

Here is my web.php code:

Route::get('changeUserStatus', [SupAdController::class, 'changeUserStatus'])->name('changeUserStatus');

Then my SupAdController.php:

public function changeUserStatus(Request $request){
    $users = User::find($request->user_id);
    $users->statusBol = $request->statusUser;
    $users->save();
}

The toggle button can be clicked but the status on the database wont change.

  • dd($request->all()); in your controller and check what you are getting in 'statusUser' – Aqib Javed Apr 13 '22 at 05:22
  • 2
    Check browser devtools - does the network request happen? Is the payload right? What is the server response? BTW [convention is to use POST when changing data](https://stackoverflow.com/questions/3477333/what-is-the-difference-between-post-and-get). – Don't Panic Apr 13 '22 at 05:38
  • Nothings happening after I tried dd($request->all()); – user18742698 Apr 13 '22 at 05:49

0 Answers0