Confirm delete with laravel form?

David picture David · Oct 29, 2014 · Viewed 41.9k times · Source

Problem with confirm delete, all works fine, but if you click cancel in confirm popup it will delete the item from DB. Any ideas? Please show with code example, I'm new to laravel.

<script>

  function ConfirmDelete()
  {
  var x = confirm("Are you sure you want to delete?");
  if (x)
    return true;
  else
    return false;
  }

</script>

{!! Form::open(['method' => 'DELETE', 'route' => ['path_delete_time', $time->id], 'onsubmit' => 'ConfirmDelete()']) !!}

  {!! Form::hidden('case_id', $project->id, ['class' => 'form-control']) !!}

  {!! Form::button('<i class="glyphicon glyphicon-trash"></i>', array('type' => 'submit', 'class' => 'specialButton')) !!}

{!! Form::close() !!}

Answer

lukasgeiter picture lukasgeiter · Oct 29, 2014

Just add return to your onsubmit call. so the value the function returns determines if the form gets submitted or not.

'onsubmit' => 'return ConfirmDelete()'