Laravel : Confirm dialog for delete button

Vineeth Vijayan
1 min readSep 13, 2017

--

As a beginner in web development and Laravel, it used to be a problem for adding a confirmation before deleting action in a form post. In the development stage, the requirement for confirmation may go unnoticed, but later when we tried to add it, it will end up being a mess. I used to convert the whole form post to ajax request for setting up confirm dialog.

So if any beginner out there ends up stuck on this confirmation dialog functionality, you can go through this trick. I hope this saves you some time.

@foreach($users as $user)
<form class="delete" action="[delete route]" method="POST">
<input type="hidden" name="_method" value="DELETE">
{{ csrf_field() }}
<input type="submit" value="Delete User">
</form>
@endforeach
<script>
$(".delete").on("submit", function(){
return confirm("Are you sure?");
});
</script>

If you are not a great fan of standard javascript confirm method, you can check out some beautiful alternative of the same like SweetAlert.

Happy Coding !!

Love this article? Click here to buy me a coffee :)

--

--

Vineeth Vijayan

Software Engineer | Scribbler | Loves coding and movies | Passive Reader | Lazy Blogger