【问题标题】:Sweetalert2 delete confirmationSweetalert2 删除确认
【发布时间】:2020-01-17 09:40:26
【问题描述】:

我想在 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


    【解决方案1】:

    Jangan lupa 包含 jQuery 基拉 - 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/sweetalert2@9.6.1/dist/sweetalert2.all.min.js"></script>
    
    <button onclick="hapus(10)" style="width:70px" type="button" class="btn btn-block btn-outline-danger">Hapus</button>

    【讨论】:

    • udah bisa di klik yes gan tapi abis tu oops 出了点问题 datanya juga ga kehapus
    • Sesuaikan 链接 urlnya ``` url : postingan/hapus/${$___id}, ```
    【解决方案2】:

    试试这个

    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;
        }
    

    【讨论】:

    • 按钮怎么样
    【解决方案3】:

    试试这个

    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>
    

    【讨论】:

      【解决方案4】:

      我想尝试回答,因为现在我遇到了同样的问题,用甜蜜警报确认删除数据。所以我使用 ajax(post) 的方法和工作。

      首先在按钮中获取id数据

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

      然后让函数delete调用sweetalert

      function del(id){
        Swal.fire({
        title: 'Yakin menghapus?',
        text: "Data yang sudhah dihapus tidak dapat dikembalikan!",
        icon: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#3085d6',
        cancelButtonColor: '#d33',
        confirmButtonText: 'Ya, hapus sekarang!'
        }).then((result) => {
          if (result.value) {
            Swal.fire({
              title: 'Terhapus!',
              text: 'Data berhasil dihapus.',
              icon: 'success',
              showConfirmButton: false
            });
            $.ajax({
              type:"POST",
              url: "back/rule/kontak_blog/del", //url function delete in controller
              data:{
                id:id //id get from button delete
              },
              success:function(data){ //when success will reload page after 3 second
               window.setTimeout( function(){ 
                   location.reload();
               }, 300 );
              }
            });
          }
        })
      }
      

      控制器删除

      function del() {
          $id = $this->input->post('id'); //get data from ajax(post)
          $del = $this->M_kontak_blog->del($id);
      }
      

      模型删除

      function del($id) {
          $this->db->where('kb_id', $id);
          $this->db->delete(' tb_kontak_blog');
      }
      

      希望这个回答对你有帮助

      【讨论】:

        猜你喜欢
        • 2021-12-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-11-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多