简体   繁体   中英

Sweet Alert js on Codeigniter

Anyone ever using sweet alert js? I have a question.

this is my code to delete a post by id_event:

 <a href="<?php echo base_url(); ?>admin/event/delete_event/<?php echo $row->id_event; ?>" title="delete"><i class="glyphicon glyphicon-trash" onClick="return(confirm('Are you sure?'));"></i></a> 

i want to using sweet alert js for delete confirmation. this is the code :

swal({
  title: "Are you sure?",
  text: "Your will not be able to recover this post!",
  type: "warning",
  showCancelButton: true,
  confirmButtonColor: "#DD6B55",
  confirmButtonText: "Yes, delete it!",
  closeOnConfirm: false
},
function(){
  swal("Deleted!", "Your post has been deleted.", "success");
});

i want to delete the post when i click the confirmButton. how do i implementation the code? thanks for helping :)

You need use JS to prevent default action on this link and send AJAX request to server.

Something like this:

$('a[title=delete]').on('click', function(e) {
   e.preventDefault();
   $.ajax({
    url: 'admin/event/delete_event/'+$(@).attr('data-id'),
    method: "POST",
    success: function() {
     //code when success
     },
    error: function() {
     //code when error
    }
});

Unfortunately sweetAlert not have its own methods for closing modal. So I'm just returning closeModal method in the closure sweetAlert.

If you don't want to use ajax, you can't understand when delete is success or fail

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