CodeIgniter Forums
Need Idea for confirm before delete using confirm - Printable Version

+- CodeIgniter Forums (
+-- Forum: General (
+--- Forum: Lounge (
+--- Thread: Need Idea for confirm before delete using confirm (/showthread.php?tid=69967)

Need Idea for confirm before delete using confirm - googlemy - 02-04-2018

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

<script type="text/javascript">
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
if (confirm) {
    swal("Deleted!", "Your imaginary file has been deleted.", "success");
} else {
    swal("Cancelled", "Your imaginary file is safe :)", "error");



RE: Need Idea for confirm before delete using confirm - wishmaster - 02-04-2018

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

RE: Need Idea for confirm before delete using confirm - InsiteFX - 02-04-2018

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.

RE: Need Idea for confirm before delete using confirm - dave friend - 02-04-2018

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.

<script src=""></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:
('BASEPATH') OR exit('No direct script access allowed');

Swal_test extends CI_Controller
    public function 

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

The view file: swaltest_v.php

PHP Code:
<!DOCTYPE html>
script src=""></script>
title>SWAL Confirm test</title>        
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> 
            document.getElementById("delete-btn").addEventListener("click", function (event)
                    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");

Hope this helps.