Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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() !!}
like image 525
David Avatar asked Oct 29 '14 08:10

David


People also ask

How do I delete a record in laravel?

php. Route::get('delete-records','StudDeleteController@index'); Route::get('delete/{id}','StudDeleteController@destroy'); Step 6 −The output will appear as shown in the following image. Step 7 − Click on delete link to delete that record from database.

How do you delete in laravel eloquent?

Laravel Eloquent Deleting To delete a model instance, retrieve it and call the delete() method: $user = User::find(1); $user->delete(); Alternatively, you can specify a primary key (or an array of primary keys) of the records you wish to delete via the destroy() method: User::destroy(1); User::destroy([1, 2, 3]);


2 Answers

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

'onsubmit' => 'return ConfirmDelete()'
like image 105
lukasgeiter Avatar answered Oct 02 '22 11:10

lukasgeiter


Simple put jQuery code for prompt box when submit form or delete record.

jQuery(document).ready(function($){
     $('.deleteGroup').on('submit',function(e){
        if(!confirm('Do you want to delete this item?')){
              e.preventDefault();
        }
      });
});
like image 34
Dhaval Tailored Avatar answered Oct 02 '22 12:10

Dhaval Tailored