Sweetalert2 删除确认

我想在 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');

    }


qq_遁去的一_1
浏览 293回答 3
3回答

SMILET

不要忘记像这样包含 jQuery Calculatefunction hapus($___id) {&nbsp;&nbsp;&nbsp; swal.fire({&nbsp; &nbsp; &nbsp; &nbsp; title: 'Are you sure?',&nbsp; &nbsp; &nbsp; &nbsp; text: "Are you sure you want to proceed ?",&nbsp; &nbsp; &nbsp; &nbsp; type: 'warning',&nbsp; &nbsp; &nbsp; &nbsp; showCancelButton: true,&nbsp; &nbsp; &nbsp; &nbsp; confirmButtonText: 'Yes'&nbsp; &nbsp; }).then(function(result) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if (result.value) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $.ajax({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url : `postingan/hapus/${$___id}`,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type : 'GET',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dataType:'json',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; beforeSend: function() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; swal.fire({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; title: 'Please Wait..!',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text: 'Is working..',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; onOpen: function() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; swal.showLoading()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; success : function(data) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; swal.fire({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; position: 'top-right',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type: 'success',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; title: 'User&nbsp; deleted successfully',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; showConfirmButton: false,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; timer: 2000&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; complete: function() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; swal.hideLoading();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; error: function(jqXHR, textStatus, errorThrown) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; swal.hideLoading();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; swal.fire("!Opps ", "Something went wrong, try again later", "error");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });}<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>

蓝山帝景

我想尝试回答,因为现在我遇到了同样的问题,用甜蜜警报确认删除数据。所以我使用带有ajax(post)的方法并工作。首先在按钮中获取id数据&nbsp;<button onclick="hapus(<?php echo $b->id; ?>)" style="width:70px"type="button" class="btn btn-block btn-outline-danger"><?php echo 'Hapus');&nbsp; ?></button>然后使函数删除调用sweetalertfunction del(id){&nbsp; Swal.fire({&nbsp; title: 'Yakin menghapus?',&nbsp; text: "Data yang sudhah dihapus tidak dapat dikembalikan!",&nbsp; icon: 'warning',&nbsp; showCancelButton: true,&nbsp; confirmButtonColor: '#3085d6',&nbsp; cancelButtonColor: '#d33',&nbsp; confirmButtonText: 'Ya, hapus sekarang!'&nbsp; }).then((result) => {&nbsp; &nbsp; if (result.value) {&nbsp; &nbsp; &nbsp; Swal.fire({&nbsp; &nbsp; &nbsp; &nbsp; title: 'Terhapus!',&nbsp; &nbsp; &nbsp; &nbsp; text: 'Data berhasil dihapus.',&nbsp; &nbsp; &nbsp; &nbsp; icon: 'success',&nbsp; &nbsp; &nbsp; &nbsp; showConfirmButton: false&nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; $.ajax({&nbsp; &nbsp; &nbsp; &nbsp; type:"POST",&nbsp; &nbsp; &nbsp; &nbsp; url: "back/rule/kontak_blog/del", //url function delete in controller&nbsp; &nbsp; &nbsp; &nbsp; data:{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; id:id //id get from button delete&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; success:function(data){ //when success will reload page after 3 second&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;window.setTimeout( function(){&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;location.reload();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}, 300 );&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }&nbsp; })}控制器删除function del() {&nbsp; &nbsp; $id = $this->input->post('id'); //get data from ajax(post)&nbsp; &nbsp; $del = $this->M_kontak_blog->del($id);}模型删除function del($id) {&nbsp; &nbsp; $this->db->where('kb_id', $id);&nbsp; &nbsp; $this->db->delete(' tb_kontak_blog');}我希望这个答案能帮助你

元芳怎么了

尝试这个swal.fire({&nbsp; &nbsp; &nbsp; &nbsp; title: 'Are you sure?',&nbsp; &nbsp; &nbsp; &nbsp; text: "Are you sure you want to proceed ?",&nbsp; &nbsp; &nbsp; &nbsp; type: 'warning',&nbsp; &nbsp; &nbsp; &nbsp; showCancelButton: true,&nbsp; &nbsp; &nbsp; &nbsp; confirmButtonText: 'Yes'&nbsp; &nbsp; }).then(function(result) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if (result.value) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $.ajax({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url : 'enter your url',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type : 'POST',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data : {id:id },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dataType:'json',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; beforeSend: function() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; swal.fire({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; title: 'Please Wait..!',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text: 'Is working..',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; onOpen: function() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; swal.showLoading()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; success : function(data) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; swal.fire({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; position: 'top-right',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type: 'success',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; title: 'User&nbsp; deleted successfully',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; showConfirmButton: false,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; timer: 2000&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; complete: function() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; swal.hideLoading();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; error: function(jqXHR, textStatus, errorThrown) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; swal.hideLoading();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; swal.fire("!Opps ", "Something went wrong, try again later", "error");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });这是控制器function hapus($id){&nbsp; &nbsp; &nbsp; &nbsp; $where = array('id' => $id);&nbsp; &nbsp; &nbsp; &nbsp; $this->m_data->hapus_data($where,'blogs');&nbsp; &nbsp; &nbsp; &nbsp; //echo true; OR return true;&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP