How to use sweet alert for delete confirm in Codeigniter?

Today, We want to share with you how to use sweet alert in codeigniter.In this post we will show you sweet alert delete confirmation, hear for sweet alert delete confirm codeigniter we will give you demo and example for implement.In this post, we will learn about Laravel Sweet Alert AJAX CRUD Tutorial with an example.

How to use sweet alert for delete confirm in Codeigniter?

Step 1: Create products Table

CREATE TABLE IF NOT EXISTS `products` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `post_name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `article_info` text COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=16 ;

Step 2: Add Routes

application/config/routes.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');


$route['default_controller'] = 'welcome';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;


$route['product-list'] = 'ProductController/index';
$route['product-list/(:any)']['delete'] = "ProductController/delete/$1";

Step 3: Create ProductController Controller

application/controllers/ProductController.php

<?php


defined('BASEPATH') OR exit('No direct script access allowed');


class ProductController extends CI_Controller {


   /**
    * Get All Data from this method.
    *
    * @return Response
   */
   public function __construct() {
      parent::__construct();
      $this->load->database();
   }


   /**
    * Create from display on this method.
    *
    * @return Response
   */
   public function index()
   {
      $data['data'] = $this->db->get("products")->result();
      $this->load->view('productlist', $data);
   }


   /**
    * Delete Data from this method.
    *
    * @return Response
   */
   public function delete($id)
   {
       $this->db->delete('products', array('id' => $id));
       echo 'Deleted successfully.';
   }
}

Step 4: Create View Files

application/views/productlist.php

<!DOCTYPE html>
<html>
<head>
    <title>How to use sweet alert for delete confirm in Codeigniter? - www.pakainfo.com</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script src="https://lipis.github.io/bootstrap-sweetalert/dist/sweetalert.js"></script>
    <link rel="stylesheet" href="https://lipis.github.io/bootstrap-sweetalert/dist/sweetalert.css" />
</head>
<body>
<div class="container">
<div class="row">
    <div class="col-lg-12 margin-tb">
        <div class="pull-left">
            <h2>How to use sweet alert for delete confirm in Codeigniter? - www.pakainfo.com</h2>
        </div>
    </div>
</div>
    
<table class="table table-bordered">
  <thead>
      <tr>
          <th>Title</th>
          <th>Product Information</th>
          <th width="220px">Action</th>
      </tr>
  </thead>
  <tbody>
   <?php foreach ($data as $product) { ?>      
      <tr id="<?php echo $product->id; ?>">
          <td><?php echo $product->post_name; ?></td>
          <td><?php echo $product->article_info; ?></td>          
      <td>
          <button type="submit" class="btn btn-danger remove"> Delete</button>
      </td>     
      </tr>
   <?php } ?>
  </tbody>
</table>
</div>
    
<script type="text/javascript">
    $(".remove").click(function(){
        var id = $(this).parents("tr").attr("id");
    
       swal({
        title: "Are you sure?",
        text: "You will not be able to recover this imaginary file!",
        type: "warning",
        showCancelButton: true,
        confirmButtonClass: "btn-danger",
        confirmButtonText: "Yes, delete it!",
        cancelButtonText: "No, cancel plx!",
        closeOnConfirm: false,
        closeOnCancel: false
      },
      function(isConfirm) {
        if (isConfirm) {
          $.ajax({
             url: '/product-list/'+id,
             type: 'DELETE',
             error: function() {
                alert('Sorry dear Something is wrong');
             },
             success: function(data) {
                  $("#"+id).remove();
                  swal("Deleted!", "Your imaginary file has been deleted.", "success");
             }
          });
        } else {
          swal("Cancelled", "Your imaginary file is safe :)", "error");
        }
      });
     
    });
    
</script>
   
</body>
</html>

Free Live Chat for Any Issue

I hope you get an idea about Creating Nice Alerts with sweetAlert.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, donโ€™t forget to share.