Sweetalert2删除确认

问题描述 投票:0回答:3

我想在codeigniter中使用sweetalert2进行删除确认,但是我不知道如何使它可以有人帮助我..?

这是sweetalert2脚本

      <script src="<?php echo base_url(); ?>sweetalert2/dist/sweetalert2.all.min.js"></script>
      <script src="https://cdn.jsdelivr.net/npm/promise-polyfill"></script>
      <script src="<?php echo base_url(); ?>sweetalert2/dist/sweetalert2.min.js"></script>
      <link rel="stylesheet" href="<?php echo base_url(); ?>assets/sweetalert2/dist/sweetalert2.min.css"> 

      <script type="text/javascript">

      function hapus() {  
      event.preventDefault(); 
      var form = event.target.form; 
      Swal.fire({
      title: 'Are you sure?',
      text: "You won't be able to revert this!",
      icon: 'warning',
      showCancelButton: true,
      confirmButtonColor: '#3085d6',
      cancelButtonColor: '#d33',
      confirmButtonText: 'Yes, delete it!'
    }).then((result) => {
      if (result.value) {
        Swal.fire(
          'Deleted!',
          'Your file has been deleted.',
          'success'
        )
      }
    })}
    </script>

这是删除按钮

 <button onclick="hapus()" style="width:70px"type="button" class="btn btn-block btn-outline-danger"><?php echo anchor('postingan/hapus/'.$b->id,'Hapus');  ?></button>

这是控制器

function hapus($id){
        $where = array('id' => $id);
        $this->m_data->hapus_data($where,'blogs');
        redirect('Postingan');
    }
php codeigniter sweetalert2
3个回答
0
投票

尝试一下

swal.fire({
        title: 'Are you sure?',
        text: "Are you sure you want to proceed ?",
        type: 'warning',
        showCancelButton: true,
        confirmButtonText: 'Yes'

    }).then(function(result) { 
        if (result.value) {
            $.ajax({
                url : 'enter your url',
                type : 'POST',
                data : {id:id },
                dataType:'json',
                beforeSend: function() {
                    swal.fire({
                        title: 'Please Wait..!',
                        text: 'Is working..',
                        onOpen: function() {
                            swal.showLoading()
                        }
                    })
                },
                success : function(data) { 
                    swal.fire({
                        position: 'top-right',
                        type: 'success',
                        title: 'User  deleted successfully',
                        showConfirmButton: false,
                        timer: 2000
                    });
                 },
                complete: function() {
                    swal.hideLoading();
                },
                error: function(jqXHR, textStatus, errorThrown) {
                    swal.hideLoading();
                    swal.fire("!Opps ", "Something went wrong, try again later", "error");
                }
            });
        }
    });

这是控制器

function hapus($id){
        $where = array('id' => $id);
        $this->m_data->hapus_data($where,'blogs');
        //echo true; OR return true;
    }

0
投票

尝试一下

function hapus($___id) {  
  swal.fire({
        title: 'Are you sure?',
        text: "Are you sure you want to proceed ?",
        type: 'warning',
        showCancelButton: true,
        confirmButtonText: 'Yes'
    }).then(function(result) { 
        if (result.value) {
            $.ajax({
                url : `postingan/hapus/${$__id}`,
                type : 'GET',
                dataType:'json',
                beforeSend: function() {
                    swal.fire({
                        title: 'Please Wait..!',
                        text: 'Is working..',
                        onOpen: function() {
                            swal.showLoading()
                        }
                    })
                },
                success : function(data) { 
                    swal.fire({
                        position: 'top-right',
                        type: 'success',
                        title: 'User  deleted successfully',
                        showConfirmButton: false,
                        timer: 2000
                    });
                 },
                complete: function() {
                    swal.hideLoading();
                },
                error: function(jqXHR, textStatus, errorThrown) {
                    swal.hideLoading();
                    swal.fire("!Opps ", "Something went wrong, try again later", "error");
                }
            });
        }
    });
}

按钮

<button onclick="hapus(<?php echo $b->id;  ?>)" style="width:70px"type="button" class="btn btn-block btn-outline-danger">Hapus</button>

0
投票

Jangan lupa包含jQueryKira-Kira Seperti ini

function hapus($___id) {  
  swal.fire({
        title: 'Are you sure?',
        text: "Are you sure you want to proceed ?",
        type: 'warning',
        showCancelButton: true,
        confirmButtonText: 'Yes'
    }).then(function(result) { 
        if (result.value) {
            $.ajax({
                url : `postingan/hapus/${$___id}`,
                type : 'GET',
                dataType:'json',
                beforeSend: function() {
                    swal.fire({
                        title: 'Please Wait..!',
                        text: 'Is working..',
                        onOpen: function() {
                            swal.showLoading()
                        }
                    })
                },
                success : function(data) { 
                    swal.fire({
                        position: 'top-right',
                        type: 'success',
                        title: 'User  deleted successfully',
                        showConfirmButton: false,
                        timer: 2000
                    });
                 },
                complete: function() {
                    swal.hideLoading();
                },
                error: function(jqXHR, textStatus, errorThrown) {
                    swal.hideLoading();
                    swal.fire("!Opps ", "Something went wrong, try again later", "error");
                }
            });
        }
    });
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/sweetalert2.all.min.js"></script>

<button onclick="hapus(10)" style="width:70px" type="button" class="btn btn-block btn-outline-danger">Hapus</button>
© www.soinside.com 2019 - 2024. All rights reserved.