jhutto
259
5
Laravel

Delete Confirmation in form button

Posted 8 months ago by jhutto

I have been working on a project and have a question about setting up a delete confirmation with the form. I can make it with a standard html form as show below, but using Laraval blade form becomes a whole other thing. I have search quite a bit and have found javascript solutions but I just wonder, why would I use javascript when I can just use the standard form creation and it works. I would think that laravel would have create this as standard. But I may just not have been able to find the right information. I would love some feedback. What's the best way?????

Here's is the html form code that works.....

@foreach($instructors as $key => $instructor)
    
    <tr>
    <td>{{$instructor->FirstName}} {{$instructor->LastName}}</td>
    <div class="btn-group">
    <td class="text-center"><a href="/instructors/{{$instructor->id}}/edit" id="btnEdit" class="btn btn-primary btn-lg" role="button">&nbsp; Edit &nbsp;</a></td>
    <td class="text-center">
            <form onsubmit="return confirm('Please confirm you want to delete! {{$instructor->FirstName}} {{$instructor->LastName}} ');" action="/instructors/{{$instructor->id}}" method="POST" >
            {{ csrf_field() }}
            {{ Method_field ('Delete')}}
            <button  id="btnDelete" type="submit" class="btn btn-warning btn-lg">Delete</button>
            </form>
    
    
    </div>
    </td>
    </tr>
      

    
@endforeach

And here is the blade form that I'm trying to get to work with javascript. The only issue is getting the name of what they are deleting into the confirmation dialog. Haven't been successful in making that work. I've been trying with hidden form field.

  @foreach ($permissions as $key => $permission)
    <tr>
        <td>{{ $permission->name }}</td>
        <td>{{ $permission->guard_name }}</td>
        <td>
        <div class="btn-group">
                &nbsp; 
            @role('Admin')
                <a class="btn btn-primary btn-lg" role="button" href="{{ route('permissions.edit', $permission->id) }}">Edit</a>
                &nbsp; 
                &nbsp; 
            @endrole
            @role('Admin')
                {{ Form::open([ 'method' => 'DELETE', 'route' => ['permissions.destroy', $permission->id ], 'onsubmit' => 'return ConfirmDelete()']) }}
                {!! Form::hidden('permissionname', $permission->name ,[ 'id' => $permission->name ]) !!}
                {{ Form::submit('Delete', ['class' => 'btn btn-danger btn-lg']) }}
                {{ Form::close() }}

            @endrole
        </div>
        </td>
     
    </tr>
    @endforeach

Javascript

<script type="text/javascript">
    var ctext = 'Confirm you want to Delete ? \n'
    var permissiontext = document.getElementsByName('permissionname');

    console.log(ctext);
    console.log(permissiontext);
    function ConfirmDelete(){
        
        return confirm(ctext);
        };
      
</script>

Please sign in or create an account to participate in this conversation.