NenTang.vn
Thiết kế Lập trình Web BackendLập trình căn bản PHP
Tạo chức năng CRUD (Xem-Read, Thêm-Create, Sửa-Edit, Xóa-Delete) bằng kỹ thuật AJAX cho danh mục Phẳng - Delete
Chương 15 · Bài 8
Thao tác với cơ sở dữ liệu MySQL bằng kỹ thuật AJAX
Thời lượng: 51 phútĐọc khoảng: 3 phút
Từ viết tắt
Nội dung bài học không khai báo từ viết tắt bằng thẻ abbr.
Nội dung bài học
Tạo chức năng Delete
php
1<!-- Nhúng file cấu hình để xác định được Tên và Tiêu đề của trang hiện tại người dùng đang truy cập -->2<?php include_once(__DIR__ . '/../../layouts/config.php'); ?>3 4<!DOCTYPE html>5<html>6 7<head>8 <!-- Nhúng file quản lý phần HEAD -->9 <?php include_once(__DIR__ . '/../../layouts/head.php'); ?>10</head>11 12<body class="d-flex flex-column h-100">13 <!-- header -->14 <?php include_once(__DIR__ . '/../../layouts/partials/header.php'); ?>15 <!-- end header -->16 17 <div class="container-fluid">18 <div class="row">19 <!-- sidebar -->20 <?php include_once(__DIR__ . '/../../layouts/partials/sidebar.php'); ?>21 <!-- end sidebar -->22 23 <main role="main" class="col-md-10 ml-sm-auto px-4 mb-2">24 <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">25 <h1 class="h2">Danh sách các Chức vụ</h1>26 </div>27 28 <!-- Block content -->29 <div id="noticeContainer" style="display: none;">30 <div class="alert alert-primary" role="alert">31 <div id="noticeMessage"></div>32 </div>33 </div>34 35 <!-- Nút tải dữ liệu, bấm vào sẽ gọi Request AJAX đến API để load dữ liệu -->36 <a href="#" id="btnLoadData" class="btn btn-success">37 Tải dữ liệu38 </a>39 <!-- Nút thêm mới, bấm vào sẽ hiển thị form nhập thông tin Thêm mới -->40 <a href="create.php" class="btn btn-primary">41 Thêm mới42 </a>43 <table id="tblList" class="table table-bordered table-hover mt-2">44 <thead class="thead-dark">45 <tr>46 <th>Mã chức vụ</th>47 <th>Tên chức vụ</th>48 <th>Tỉ lệ chức vụ</th>49 <th>Năm áp dụng chức vụ</th>50 <th>Ghi chú chức vụ</th>51 <th>Hành động</th>52 </tr>53 </thead>54 <tbody>55 </tbody>56 </table>57 <!-- End block content -->58 </main>59 </div>60 </div>61 62 <!-- footer -->63 <?php include_once(__DIR__ . '/../../layouts/partials/footer.php'); ?>64 <!-- end footer -->65 66 <!-- Nhúng file quản lý phần SCRIPT JAVASCRIPT -->67 <?php include_once(__DIR__ . '/../../layouts/scripts.php'); ?>68 69 <!-- Các file Javascript sử dụng riêng cho trang này, liên kết tại đây -->70 <script>71 $(function() {72 function getDuLieuChucVu() {73 $.ajax('/api/chucvu/get.php', {74 success: function(data) {75 var dataObj = JSON.parse(data);76 console.log(dataObj);77 var htmlString = ``;78 $.each(dataObj.data, function(index, item) {79 let aEdit = `<a href="edit.php?CV_MA=${item.CV_MA}" class="btn btn-primary"><i class="fa fa-edit"></i>Sửa</a>`;80 let aDelete = `<button type="button" data-cv_ma="${item.CV_MA}" class="btn btn-danger btnDelete"><i class="fa fa-remove"></i>Xóa</button>`;81 82 htmlString += `<tr>`;83 htmlString += `<td>${item.CV_MA}</td>`;84 htmlString += `<td>${item.CV_TEN}</td>`;85 htmlString += `<td>${item.CV_TILE}</td>`;86 htmlString += `<td>${item.CV_NAMAPDUNG}</td>`;87 htmlString += `<td>${item.CV_GHICHU}</td>`;88 htmlString += `<td>${aEdit}${aDelete}</td>`;89 htmlString += `</tr>`;90 });91 92 $('#tblList tbody').html(htmlString);93 $('#noticeMessage').html(`<span>${dataObj.msg}</span>`);94 $('#noticeMessage').parent().removeClass().addClass('alert alert-primary');95 $('#noticeContainer').css('display', 'block');96 dangKySuKienXoa();97 98 },99 error: function() {100 var htmlString = `<span>Không thể xử lý</span>`;101 $('#noticeMessage').html(htmlString);102 $('#noticeMessage').parent().removeClass().addClass('alert alert-danger');103 $('#noticeContainer').css('display', 'block');104 }105 });106 }107 108 function dangKySuKienXoa() {109 // Cảnh báo khi xóa110 // 1. Đăng ký sự kiện click cho các phần tử (element) đang áp dụng class .btnDelete111 $('.btnDelete').click(function(event) {112 let parentRowEle = $(this).closest('tr');113 114 // Click hanlder115 // Hiện cảnh báo khi bấm nút xóa116 swal({117 title: "Bạn có chắc chắn muốn xóa?",118 text: "Một khi đã xóa, không thể phục hồi....",119 icon: "warning",120 buttons: true,121 dangerMode: true,122 confirmButtonText: 'Chắc chăn XÓA!',123 cancelButtonText: "Hủy bỏ!",124 })125 .then((willDelete) => {126 if (willDelete) { // Nếu đồng ý xóa127 128 // 2. Lấy giá trị của thuộc tính (custom attribute HTML) 'CV_MA'129 // var CV_MA = $(this).attr('data-CV_MA');130 var CV_MA = $(this).data('cv_ma');131 var apiDeleteUrl = "/api/chucvu/delete.php?CV_MA=" + CV_MA;132 133 // 3. Gởi Request AJAX đếm API delete, với tham số CV_MA=?134 $.ajax(apiDeleteUrl, {135 success: function(data) {136 var dataObj = JSON.parse(data);137 console.log(dataObj);138 139 var htmlString = `<span>${dataObj.msg}</span>`;140 $('#noticeMessage').html(htmlString);141 $('#noticeContainer').css('display', 'block');142 143 if(dataObj.statusCode === 200) {144 // Xóa dòng đang chứa button (người dùng đã click XÓA) ra khỏi table145 parentRowEle.remove();146 $('#noticeMessage').parent().removeClass().addClass('alert alert-success');147 } else {148 $('#noticeMessage').parent().removeClass().addClass('alert alert-danger');149 }150 },151 error: function() {152 var htmlString = `<span>Không thể xử lý XÓA?!</span>`;153 $('#noticeMessage').html(htmlString);154 $('#noticeMessage').parent().removeClass().addClass('alert alert-danger');155 $('#noticeContainer').css('display', 'block');156 }157 });158 } else {159 swal("Thao tác đã được Hủy bỏ!");160 }161 });162 });163 };164 165 $('#btnLoadData').on('click', function(event) {166 event.preventDefault();167 getDuLieuChucVu();168 });169 170 // Init171 getDuLieuChucVu();172 });173 </script>174</body>175 176</html>
