简体   繁体   中英

Codeigniter Bootstrap Modal Delete

So I wanted to delete a row in a table using a delete button, which will then show a modal to confirm the delete process. However, I need to pass the ID, in this case I used filename, to the URL in order to complete the delete process. However, I can't seem to pass the complete URL in order to delete in the "YES" button in the modal. I am trying to simulate a process like this: Plunker

Here's the code.

Modal:

<div class="modal-body text-center">
      <p> Are you sure you want to remove this from the list? </p><br><br>
      <button type="submit" class="btn btn-custom-1">Yes</button>
      <button type="button" class="btn btn-custom" data-dismiss="modal">Cancel</button>
</div>

Delete Button:

<button type="button" class="btn btn-custom-3" data-href="<?php echo base_url() . 'admin_forms/delete_carsticker/' . $row->filename; ?>" data-toggle="modal" data-target="#delete-modal"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span>  Delete </button>

Javascript:

  $('#delete-modal').on('show.bs.modal', function(e) {
    $(this).find('.btn-custom-1').attr('href', $(e.relatedTarget).data('href'));
});

I'd use a simple global variable to store the ID.

var delete_id = ""; // or a file name

$(document).on('click','.btn-custom-3', function(){
    delete_id = $(this).attr("data-href");
    // id also open the modal via jQuery too...  
});

then when you do the YES use the delete_id.

$(document).on('click','.btn-custom-1', function(){
    if(delete_id != ""){
        // do something with it and come the modal but you can close it like you are doing it directly in the HTML...
    }
});

In codeigniter we have to use base_url() .It is MVC framework having models,views and controllers.

For delete there must be some function in controller to perform delete.

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

 class SomeClass extends CI_Controller {
   public function delete($id){
     $this->load->model('some_model');/* load model*/
      if($this->some_model->delete($id)){ /*$id is ID and delete is some function in some_model (Model)*/
        redirect('some_success_function')
       }else{
         redirect('previous_function')
       }
    }
 }

coming to html (views)

Urls will be as shown below

 <?php
if(isset($files)){
    foreach($files as $row){ 

        ?>
    <a href="#" data-href="<?php echo base_url() . 'admin_forms/delete_carsticker/' . $row->filename; ?>" data-toggle="modal" data-target="#confirm-delete">Delete record #23</a><br>

    <button class="btn btn-default" data-href="<?php echo base_url() . 'admin_forms/delete_carsticker/' . $row->filename; ?>" data-toggle="modal" data-target="#confirm-delete">
        Delete record #23
    </button>
<?php   }
}
?> 

Here SomeClass is the controller class containing function delete($id) which take $id for deleting values. Controller call models function delete($id) to perform delete action.

Edit

I have updated the code, there is no problem in model part. I have run your code in localserver no problems in javascript. Check data are coming properly from controller , verify using print_r($files)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM