PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, February 15, 2022

[FIXED] Double confirmation SweetAlert with jquery using <a>

 February 15, 2022     html, javascript, jquery, sweetalert, symfony     No comments   

Issue

I want to do a double confirmation when I want to delete an account, I really struggle for this. I checked on the forum but nothing seem to be like my problem. Im using Symfony

My function deleteAccount on my UserAccountController :

/**
 * @Route("user/account/delete", name="user_delete")
 */
public function deleteAccount(): Response{

    $user = $this->getUser();

    $this->container->get('security.token_storage')->setToken(null);

    $entityManager = $this->getDoctrine()->getManager();
    $entityManager->remove($user);
    $entityManager->flush();

    $this->addFlash(
        'success',
        'Account Deleted successfully'
    );
    
    return $this->redirectToRoute('homepage');
}

My link on the view :

<a href="{{ path('user_delete') }}" class="btn btn-danger deleteButton">Delete Account</a>

And my jquery :

$('.deleteButton').on('click', function() {

event.preventDefault();

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.isConfirmed) {

        setTimeout( function () { 
            $.get($(this).attr('href'),{});
        }, 1200);

        Swal.fire(
            'Deleted!',
            '',
            'success'
        )

    }

})

});


Solution

You have to store the delete url in a variable cause the 'this' doesn't refer anymore to the button

 $('.deleteButton').on('click', function() {
     
    event.preventDefault();
    const deleteUrl = $(this).attr('href');
    
    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.isConfirmed) {
    
            setTimeout( function () { 
                $.get(deleteUrl,{});
            }, 1200);
    
            Swal.fire(
                'Deleted!',
                '',
                'success'
            )
    
        }
    
    })

Or do like this ,

setTimeout( function () { 
                    $.get('{{ path('user_delete') }}',{});
                }, 1200);


Answered By - Rova Ram
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing