PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, March 1, 2022

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

 March 01, 2022     codeigniter, codeigniter-3, php, sweetalert2     No comments   

Issue

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]);
    }

Solution

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>



Answered By - shirshak007
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing