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
<a style="font-size: 15px;" type="submit" class="btn" onclick="delUser({{ $user->id }})">delete</a>
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)->delete(); if ($res) { $data = [ 'status' => 0, 'msg' => 'Delete successfully' ]; } else { $data = [ 'status' => 1, 'msg' => '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.