Welcome Guest, Not a member yet? Register   Sign In
Need Idea for confirm before delete using confirm
#1

Hi CI Team,

I need yours idea how to solve this,

When I click "Delete" system automatically deleted without confirm.

PHP Code:
<a href="'.site_url("admin/delete_user/" . $r->ID . "/" . $this->security->get_csrf_hash()).'" class="btn btn-danger btn-sm" onclick="return confirm();">Delete</a


Code:
<script type="text/javascript">
swal({
title: "Are you sure?",
text: "You will not be able to recover this imaginary file!",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes, delete it!",
cancelButtonText: "No, cancel plx!",
closeOnConfirm: false,
closeOnCancel: false
},
function(confirm){
if (confirm) {
    swal("Deleted!", "Your imaginary file has been deleted.", "success");
} else {
    swal("Cancelled", "Your imaginary file is safe :)", "error");
}
});
</script>

Source: https://sweetalert.js.org/

Thanks
Reply
#2

Here example I use on my project. Place below event into <button>
onclick="confirm('A you shure?') ? $('#form').submit() : false;"
Reply
#3

I always have a field in the table so that I can do soft deletes.

It will flag the record as deleted but it really is not deleted.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#4

(This post was last modified: 02-04-2018, 10:23 AM by dave friend.)

There are several problems but the first to address is the use of a whole bunch of "deprecated" options. Deprecated is in quotes because that's what Sweet Alert calls them. To me that implies "You can use them by you shouldn't", but in reality is seems they don't work at all. That's my experience when obtaining the library via the CDN.

Code:
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>

Perhaps you are using an older version that was installed using npm? If so, you should upgrade that install. What I suggest here is based on using the script tag shown above.

The next issue I see is how you define the function that responds to the dialog. Maybe you're using old syntax. The current syntax requires you attach a handler to the promise that swal() returns.

Then there is the problem of using an anchor tag to go to the delete method. The way you have it structured that link will ALWAYS be followed even if you say "No" in the confirm dialog. It's also currently considered "bad practice" to use inline JavaScript as you by defining the "onClick" property of the anchor tag.

Wow, I feel like I am really slamming you but that's not my intent. Sorry.

Below is controller and view code to demonstrate something that works and addresses all the issues I observe. I mocked up values to send to the delete method and adjusted URIs to work with this test. To the best of my understanding this does what you seek to do.

Controller Swal_test.php

PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

class 
Swal_test extends CI_Controller
{
    public function 
index()
    {
        
$this->load->view('swaltest_v');
    }

    
//OK, sure. The following is bad MVC but we're only doing a demo here - right?
    
public function delete($id$hash)
    {
        
var_dump($id$hash);
        
?>
        <a href='/swal_test'>Back</a>
        <?php
    
}


The view file: swaltest_v.php

PHP Code:
<!DOCTYPE html>
<
html>
    <
head>
        <
script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
        <
style>
            .
doit{background-color#DD6B55}
        
</style>
        <
title>SWAL Confirm test</title>        
    </
head>
    <
body>
        <?
php
        
echo date("F j, Y, g:i:s a");
        
$r           = new stdClass();
        
$r->id       "dave";
        
$deleted_uri base_url("swal_test/delete/{$r->id}/{$this->security->get_csrf_hash()}");
        
?>
        <button type="button" id="delete-btn" >Delete</button> 
        <script>
            document.getElementById("delete-btn").addEventListener("click", function (event)
            {
                swal({
                    title: "Are you sure?",
                    text: "You will not be able to recover this imaginary file!",
                    icon: "warning",
                    buttons: {
                        cancel: {
                            visible: true,
                            text: "No, cancel plx!",
                            closeModal: false,
                        },
                        confirm: {
                            text: "Yes, delete it!",
                            className: "doit",
                            closeModal: false,
                        },
                    },
                })
                    //attach to the promise returned by swal()
                    .then((willDelete) => {
                        if (willDelete) {
                            swal("Deleted!", "Your imaginary file has been deleted.", "success")
                                    //another promise and another promise fulfilled response
                                    .then(() => {
                                        //redirect to delete page complete with arguments to the method
                                        window.location.href = '<?= $deleted_uri?>';
                                    });
                        } else {
                            swal("Cancelled", "Your imaginary file is safe :)", "error");
                        }
                    });
            });
        </script>        
    </body>
</html> 

Hope this helps.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB