Skip to content
Advertisement

How to add a confirm dialog box using SweetAlert2 in CodeIgniter 3?

I’m learning CodeIgniter 3 and I want to add a confirm dialog before deleting a row in the database table. I’ve made the delete function but couldn’t figure out how to add a confirm dialog box using SweetAlert2.

code in view

<td><a href="<?php echo base_url('index.php/admin/deletestaff/' . $row->StaffUserName) ?>"
                           class="btn btn-danger btn-sm">Delete</a></td>

code in controller

public function deleteStaff($staff)
    {
        $this->load->model('Staff_Model');
        $this->Staff_Model->deleteStaff($staff);
        redirect(base_url('index.php/admin/viewstaff'));
    }

code in model

function deleteStaff($staff)
    {
        return $this->db->delete('staff', ['StaffUserName' => $staff]);
    }

Advertisement

Answer

Here is a simple integration, try and let me know

function confirm(staffname){
Swal.fire({
  title: 'Do you want to save the changes?',
  showDenyButton: true,
  showCancelButton: true,
  confirmButtonText: 'Save',
  denyButtonText: `Don't save`,
}).then((result) => {
  /* Read more about isConfirmed, isDenied below */
  if (result.isConfirmed) {
    window.location.href = "<?php echo base_url('index.php/admin/deletestaff/' ;?>" + staffname;
    Swal.fire('Saved!', '', 'success')
  } else if (result.isDenied) {
    Swal.fire('Changes are not saved', '', 'info')
  }
});
}
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

<a id="mylink" href="javascript:;" class="btn btn-danger btn-sm" onclick="return confirm('<?php echo $row->StaffUserName;?>');">Delete</a>
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement