Delete confirmation passes wrong user id in laravel, how to fix it?

I want that implement delete confirmation in laravel app when click on delete button. it works fine if there is no confirm dialoge, but does not work when I add javascript confirmation code.

here is my blade file

   <a href="{{ route('user.destroy', $user->id)}}" class="dropdown-item" onclick="
        var result = confirm('are you sure delete this?');
        if (result) {
            document.getElementById('delete_user').submit();
        }
        event.preventDefault();

        "><i class="icon-bin text-danger"></i> Delete</a>

      <form id="delete_user" action="{{ route('user.destroy', $user->id)}}" method="POST">
        @method('DELETE')
        @csrf
      </form>

1 thought on “Delete confirmation passes wrong user id in laravel, how to fix it?”

  1. Remove href attribute from <a>. And also onclick must be function.

    <a class="dropdown-item" onclick="deleteUser(event)"><i class="icon-bin text-danger"></i> Delete</a>
    

    Define delete function in the bottom of file.

    <script>
    function deleteUser(event){
      var result = confirm('are you sure delete this?');
      if (result) {
         document.getElementById('delete_user').submit();
      }
      event.preventDefault();
    
    }
    </script>
    
    Reply

Leave a Comment