Hariharan
Hariharan

Reputation: 3

Using codeigniter Confirm and delete record. Having issue in javascript confirm to delete records and redirect to the same controller/index method

I am new to codeigniter. After deleting my table record i want to redirect to same controller index method.Record deleted successfully but alert message was showing repeatedly. Instead redirect() method i am using javascript confirm method to delete it will showing alert box again and again. If i try redirect() method its not showing confirm alert box. If i try exact base_url() method to redirect it will move to URL: http://localhost/codeigniter/category/remove_category/25 and the page was empty. I tried many ways. I don't know why the confirm alert box showing repeatedly. Please advise me. Thanks in advance.

View Codes

<table border="1">  
<tbody>  
    <tr>  
        <td>Category Id</td>  
        <td>Category Name</td>
        <td>Click Edit Link</td>
        <td>Click Delete Link</td>  
    </tr>  
    <?php

    foreach ($category->result() as $row)  
    {  
        ?><tr>  
            <td><?php echo $row->category_id;?></td>  
            <td><?php echo $row->category_name;?></td>
            <td><a href="<?=base_url()?>category/edit_category/<?=$row->category_id?>">Edit</a></td>
            <td><a href="<?=base_url()?>category/remove_category/<?=$row->category_id?>">Delete</a></td>  
        </tr>  
        <?php 
    }
    ?>  
</tbody>  
</table>

Controller Codes

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

class Category extends CI_Controller {

function __construct() {
    parent::__construct();
    $this->load->model('category_model');

}

function index(){

    $data['category']=$this->category_model->show_category();  
    //return the data in view  
    $this->load->view('categories_view', $data);

}

function remove_category($category_id){

    if($this->category_model->delete_category($category_id))
    {
    /*echo "<script type='text/javascript'> confirm('Are you sure to delete permanently?');
    window.location.href='index'";*/
   //redirect(base_url().'category');

    echo "<script>
                    confirm('Are you sure to delete permanently?!');
                    window.location.href = '" . base_url() . "category';
                </script>";
    }       
}

}

Model Codes

class Category_model extends CI_Model {

function show_category(){

    $query = $this->db->get('tbl_category');
    return $query;

}

function insert_category($data){

    $this->db->insert('tbl_category', $data);
    return $this->db->insert_id();
}

function delete_category($category_id){
    $this->db->where('category_id', $category_id);
    $this->db->delete('tbl_category');
}

}

Upvotes: 0

Views: 1630

Answers (1)

Callombert
Callombert

Reputation: 1099

The proper way to do this is redirect(); Something like this:

    $this->session->set_flashdata('message', 'Category was deleted');
    redirect('category', 'refresh');

Then on Category controller:

   if ($this->session->flashdata('message')) {
      //show the message to confirm
   }

Finally, if you want javascript confirm: so when the user clicks, it asks to confirm and then goes to delete if the user clicks okay. You're asking confirmation when the row has already been deleted.

  <td><a onclick="return confirm('are you sure?')" href="<?=base_url()?>category/remove_category/<?=$row->category_id?>">Delete</a></td>  

Upvotes: 1

Related Questions