SoFunction
Updated on 2025-04-04

Example of Laravel framework based on ajax and implementing refresh-free delete function

This article describes the Laravel framework based on ajax and implements refresh-free deletion function. Share it for your reference, as follows:

1. First, introduce

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

2. Add events to the delete button

&lt;a style="font-size: 15px;" type="submit" class="btn" onclick="delUser({{ $user-&gt;id }})"&gt;delete&lt;/a&gt;

3. Content of the event

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

4. The above events are transmitted to the method content

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

5. Complete

For more information about Laravel, readers who are interested in view the topic of this site:Laravel framework introduction and advanced tutorial》、《Summary of excellent development framework for php》、《PHP object-oriented programming tutorial》、《PHP+mysql database operation tutorial"and"Summary of common database operation techniques for php

I hope that this article will be helpful to everyone's PHP programming based on the Laravel framework.