(2023) PHP+MySqli EP.43 How to AJAX CrudPHP MySqli (Part 3 Delete)
(2023) PHP+MySqli EP.43
How to AJAX CrudPHP MySqli
(Part 3 Delete)
ระบบบริหารจัดการ ศูนย์ซ่อมรถยนต์ Car Service System ( PHP 7++ ขึ้นไป )
(2023) PHP+MySqli EP.42 How to AJAX CrudPHP MySqli (Part 2 Update)
ajax_view.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<a href="#deleteEmployeeModal" class="delete" data-id="<?php echo $row["id"]; ?>" data-toggle="modal"><i class="material-icons" data-toggle="tooltip" title="Delete"></i></a> <!-- Delete Modal HTML --> <div id="deleteEmployeeModal" class="modal fade"> <div class="modal-dialog"> <div class="modal-content"> <form> <div class="modal-header"> <h4 class="modal-title">Delete User</h4> <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> </div> <div class="modal-body"> <input type="hidden" id="id_d" name="id" class="form-control"> <p>Are you sure you want to delete these Records?</p> <p class="text-warning"><small>This action cannot be undone.</small></p> </div> <div class="modal-footer"> <input type="button" class="btn btn-default" data-dismiss="modal" value="Cancel"> <button type="button" class="btn btn-danger" id="delete">Delete</button> </div> </form> </div> </div> </div> |
ajax_crud.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
$(document).on("click", ".delete", function() { var id=$(this).attr("data-id"); $('#id_d').val(id); }); $(document).on("click", "#delete", function() { $.ajax({ url: "save_db.php", type: "POST", cache: false, data:{ type:3, id: $("#id_d").val() }, success: function(dataResult){ $('#deleteEmployeeModal').modal('hide'); $("#"+dataResult).remove(); } }); }); |
save_db.php
1 2 3 4 5 6 7 8 9 10 11 12 13 |
if(count($_POST)>0){ if($_POST['type']==3){ $id=$_POST['id']; $sql = "DELETE FROM `crud` WHERE id=$id "; if (mysqli_query($conn, $sql)) { echo $id; } else { echo "Error: " . $sql . "<br>" . mysqli_error($conn); } mysqli_close($conn); } } |