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 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>
CodePudding user response:
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 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");
CodePudding user response:
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" >
<i ></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 :)