Confirm deletion in Laravel 5.4

0

I have the form below, in which a button deletes the record of the current line.

<form  class="form-inline" method="POST" action="/servidores/{{ $serve->id }}">
        {{ method_field('DELETE') }}
    <input type="hidden" name="_token" value="{{ csrf_token() }}">
    <button type="submit" class="btn btn-xs btn-danger"'>Excluir</button>
</form>

It works, but does not confirm the deletion; deletes onclick. Is it possible to add confirmation with laravel features?

    
asked by anonymous 06.02.2017 / 14:16

1 answer

1

Use confirm of Javascript by putting it in onsubmit of your form for this.

See:

<form  class="form-inline" method="POST" action="/servidores/{{ $serve->id }}" onsubmit="confirm('Tem certeza que deseja excluir?')">
        {{ method_field('DELETE') }}
    <input type="hidden" name="_token" value="{{ csrf_token() }}">
    <button type="submit" class="btn btn-xs btn-danger"'>Excluir</button>
</form>

You can still do more sophisticated, separating logic in a Javascript file.

See an example:

$('#formulario').on('submit', function () {

     var confirmado = confirm('Deseja deletar esses dados?');

     if (! confirmado) return false;
});

In this last example, you would need to put the id attribute with the formulário value on your form .

If you are going to use pure javascript, just do so:

document.querySelector('#formulario').addEventListener('submit', function () {
      // mesmo código anterior
});
    
06.02.2017 / 14:42