Example of Laravel framework implementing refresh free deletion function based on ajax and layer. js

  • 2021-11-13 01:05:02
  • OfStack

This article illustrates the implementation of refresh-free deletion based on ajax and layer. js in Laravel framework. Share it for your reference, as follows:

1. First, introduce layer. js


<script type="text/javascript" src="{{ asset('/public/bootstrap/js/jquery-3.2.1.min.js') }}"></script>
<script type="text/javascript" src="{{ asset('/public/layer/layer.js') }}"></script>

2. Add an event to the delete button


<a style="font-size: 15px;" type="submit" class="btn" onclick="delUser({{ $user->id }})"> Delete </a>

3. Content of the event


function delUser(user_id)
{
  layer.confirm(' Are you sure you want to delete me? ', {  //  Use layer.js Confirmation pop-up window 
    btn: [' Determine ', ' Cancel '],
  }, function() {            //  Executed when determined 
    $.post("{{ url('user') }}/" + user_id, {  //  Website, data, operation after success 
      "_token": "{{ csrf_token() }}",
      "_method": "delete"
    }, function(data) {
      if (data.status == 0) {
        layer.msg(data.msg, { icon: 6});
        location.href = "{{ url('user/index') }}";
      } else {
        layer.msg(data.msg, { icon: 5});
      }
    });
  }, function() {});
}

4. The above events are transmitted to the method content


public function destroy($user_id)
{
    $res = User::find($user_id)->delete();
    if ($res) {
      $data = [
        'status' => 0,
        'msg' => ' Delete succeeded '
      ];
    } else {
      $data = [
        'status' => 1,
        'msg' => ' Delete failed '
      ];
    }
    return $data;
}

5. Complete

More readers interested in Laravel can check the topics of this site: "Introduction and Advanced Tutorial of Laravel Framework", "Summary of Excellent Development Framework of php", "Introduction Tutorial of php Object-Oriented Programming", "Introduction Tutorial of php+mysql Database Operation" and "Summary of Common Database Operation Skills of php"

I hope this article is helpful to the PHP programming based on Laravel framework.


Related articles: