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

(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.

<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:
('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="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></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.

Messages In This Thread
RE: Need Idea for confirm before delete using confirm - by dave friend - 02-04-2018, 10:08 AM

Theme © iAndrew 2016 - Forum software by © MyBB