May Sale! All accounts are 40% off this week.

jhutto's avatar

Delete Confirmation in form button

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>
0 likes
5 replies
Talinon's avatar

I don't see why you can't do almost exactly what you had originally.

Does this work?

{{ Form::open([ 'method' => 'DELETE', 'route' => ['permissions.destroy', $permission->id ], 'onsubmit' => "return ConfirmDelete('Please confirm you want to delete! {{ $permission->name }}')"]) }}
jhutto's avatar

If works fine. But I'm new to laravel and trying to learn the best way of doing things. So, that's why I posted this... Just wanted to make sure I wasn't missing something. Thanks for the response.

Talinon's avatar

Both examples you had above are using blade. Perhaps you mean why bother using the laravelcollective/html package?

If so, that is certainly up to you. I know a lot of developers have stopped using it, especially since it's not even included out-of-the-box with Laravel anymore. Many just opt to write out the markup themselves. You can definitely go that route and simply not use it.

I personally use it sometimes, depending upon the project. Usually because of how easy it makes dealing with some SELECT elements.

1 like
munazzil's avatar

Use as like below for javascript easy pop up

     <button  id="btnDelete" type="submit" onclick= "return confirm('Are You Sure Want to Delete?')" class="btn btn-warning btn-lg">Delete</button>

Please or to participate in this conversation.