(2023) PHP+MySqli EP.42 How to AJAX CrudPHP MySqli (Part 2 Update)
(2023) PHP+MySqli EP.42
How to AJAX CrudPHP MySqli
(Part 2 Update)
ระบบบริหารจัดการ ศูนย์ซ่อมรถยนต์ 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 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<a href="#editEmployeeModal" class="edit" data-toggle="modal"> <i class="material-icons update" data-toggle="tooltip" data-id="<?php echo $row["id"]; ?>" data-name="<?php echo $row["name"]; ?>" data-email="<?php echo $row["email"]; ?>" title="Edit"></i> </a> <!-- Edit Modal HTML --> <div id="editEmployeeModal" class="modal fade"> <div class="modal-dialog"> <div class="modal-content"> <form id="update_form"> <div class="modal-header"> <h4 class="modal-title">Edit User</h4> <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> </div> <div class="modal-body"> <input type="hidden" id="id_u" name="id" class="form-control" required> <div class="form-group"> <label>Name</label> <input type="text" id="name_u" name="name" class="form-control" required> </div> <div class="form-group"> <label>Email</label> <input type="email" id="email_u" name="email" class="form-control" required> </div> </div> <div class="modal-footer"> <input type="hidden" value="2" name="type"> <input type="button" class="btn btn-default" data-dismiss="modal" value="Cancel"> <button type="button" class="btn btn-info" id="update">Update</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 22 23 24 25 26 27 28 |
$(document).on('click','.update',function(e) { var id=$(this).attr("data-id"); var name=$(this).attr("data-name"); var email=$(this).attr("data-email"); $('#id_u').val(id); $('#name_u').val(name); $('#email_u').val(email); }); $(document).on('click','#update',function(e) { var data = $("#update_form").serialize(); $.ajax({ data: data, type: "post", url: "save_db.php", success: function(dataResult){ var dataResult = JSON.parse(dataResult); if(dataResult.statusCode==200){ $('#editEmployeeModal').modal('hide'); alert('Data updated successfully !'); location.reload(); } else if(dataResult.statusCode==201){ alert(dataResult); } } }); }); |
save_db.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
if(count($_POST)>0){ if($_POST['type']==2){ $id=$_POST['id']; $name=$_POST['name']; $email=$_POST['email']; $sql = "UPDATE `crud` SET `name`='$name',`email`='$email' WHERE id=$id"; if (mysqli_query($conn, $sql)) { echo json_encode(array("statusCode"=>200)); } else { echo "Error: " . $sql . "<br>" . mysqli_error($conn); } mysqli_close($conn); } } |