David David - 26 days ago 17
PHP Question

Confirm delete with laravel form?

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

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

'onsubmit' => 'return ConfirmDelete()'