Confirm exclusion in Laravel 5.4

Asked

Viewed 1,256 times

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. You can add confirmation with the Standard features?

1 answer

1

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

Behold:

<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 it more sophisticated by separating the logic in a Javascript file.

Take 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 attribute id with the value formulário in his form.

If using pure javascript, just do so:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.