skip to Main Content

I laravel I am trying to create a logout confirmation message when I click on my logout button
for this I have done this to my navebar.blade.php

<a class="dropdown-item" id="logout" href="{{ route('admin.logout') }}">
{{ __('Logout') }}
</a>

and this to my admin.blade.php but not working but it should work

<script>
        $(docoment).on("click", "#logout", function(e) {
            e.preventDefault();
            var link = $(this).attr("href");
            swal({
                    title: "Are you Want to logout?",
                    text: "",
                    icon: "warning",
                    buttons: true,
                    dragerMode: true,
                })
                .then((willDelete) => {
                    if (willDelete) {
                        window.location.href = link;
                    } else {
                        swal("Not Logout");
                    }
                });
        });
    </script>

2

Answers


  1. Here the <a> tag doesn’t waits for the click function to execute. It will redirects to the link in href. So change the function as follows.

    <a class="dropdown-item" id="logout" data-link="{{ route('admin.logout') }}">
        {{ __('Logout') }}
    </a>
    <script>
        $(docoment).on("click", "#logout", function(e) {
            e.preventDefault();
            var link = $(this).attr("data-link");
            swal({
                    title: "Are you Want to logout?",
                    text: "",
                    icon: "warning",
                    buttons: true,
                    dragerMode: true,
                })
                .then((willDelete) => {
                    if (willDelete) {
                        window.location.href = link;
                    } else {
                        swal("Not Logout");
                    }
                });
        });
    </script>
    

    If you are using jQuery >= 1.4.3, then you can also get the data attribute like

    var link = $(this).data("link");
    
    Login or Signup to reply.
  2. Well I’d prefer to create a form to logout. Get method for logging out is not recommended.

    <form action="{{ route('logout') }}" method="post" id="logoutorm">
      @csrf
       <button type="submit" class="dropdown-item">
          <i class="far fa-fw fa-arrow-alt-circle-left mr-1"></i> {{ __('Sign Out') }}
       </button>
     </form>
    

    To submit this form via sweet alert you can do something like this:

    <script>
    $(document).on("submit", "#logoutorm", function(event) {
        event.preventDefault();
        let form = $(this);
        swal({
                title: "Are you Want to logout?",
                text: "",
                icon: "warning",
                buttons: true,
                dragerMode: true,
            })
            .then((willDelete) => {
                if (willDelete) {
                    form.submit();
                } else {
                    swal("Logout Canceled");
                }
            });
    });
    

    Hope this would work for you 🙂

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search