0

I've got some problem with confirmation of deleting data in Laravel. Here's code. You can see the icon whick respond for deleting and jquery method which i have added to form in class deleteGroup. It does not work. Window does not appear and form deletes element without asking.

<a href="" onclick="event.preventDefault(); document.getElementById('destroy-form-{{$value->id}}').submit();">
 <i class="material-icons delete-icon">delete</i></a>
<form class="deleteGroup" id="destroy-form-{{$value->id}}"

action="{{route('lecture.destroy',['id'=>$value->id])}}"method="POST" style="display: none;">
<input type="hidden" name="_method" value="DELETE"></input>
@csrf
</form>                                        
<script>
jQuery(document).ready(function ($) {
                                    $('.deleteGroup').on('submit', function (e) {
                                     if (!confirm('Do you want to delete this item?')) {
                                                        e.preventDefault();
                                                    }
                                                });
                                            });
</script>

LectureController:

    public function destroy($id)
    {
        $lectures = Lecture::findOrFail($id);
        $lectures->delete();
        Session::flash('success', 'Deleted: ' . $lectures->name . '!');

        return redirect()->route('lecture.index');
    }

Can someone help?

2
  • try with $('.deleteGroup').on('submit', (e)=>{alert("submit"); e.preventDefault()}); and see if this function is fired Commented Nov 14, 2019 at 22:51
  • if i use this script, it works fine... try adding a id to the form and use $('#formid').on('submit', function (e){}); and change formid with the real id of the form Commented Nov 14, 2019 at 22:53

3 Answers 3

1

Laravel has the cool feature in HTML form. You can simply do this. Here in this code i have deleted person entity.

{!! Form::open(['route' => ['people.destroy', $person->id], 'method' => 'delete']) !!}
  {!! Form::button('<i class="glyphicon glyphicon-trash">Delete</i>',
 ['type' => 'submit',
 'class' => 'btn btn-danger',
 'onclick' => "return confirm('Are you sure?')"]) !!}
{!! Form::close() !!}

it will give you the alert section like this. enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

Can i do this without button and with icon as I had it before ?
Yes. Just u need is to remove the Delete text inside <i>tag
0

Hi you can try without Form.

// confirm to delete
$('.delete').on('click', function() {
  // var question= confirm("Do you really want me delete this?");
  if (confirm('Are you sure?')) {} else {
    return false;
  }
});


// delete fact
function removeFact(fact_id, author_id) {

  if (fact_id == '') {
    alert('No Fact Id');
    return;
  }

  $.ajax({
    url: "{{ route('product.author.fact.delete') }}",
    method: 'POST',
    data: {
      fact_id: fact_id,
      author_id: author_id
    },
    dataType: 'json',
    beforeSend: function() {
      $('#facts-load').html(`@include('models.loader')`);
    },
    success: function(data) {
      if (data == '') {
        $('#facts-load').html('<p class="alert alert-danger">No Result</p>');
        return;
      }
      $('#facts-load').html(data);
    },
    fail: function(e) {
      console.log(e);
    }
  })
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="col-md-2">
  <button type="button" class="btn btn-danger w-100 delete" onclick="removeFact('{{ $val->id }}', '{{ $author->id }}')"><i class="fa fa-minus" aria-hidden="true"></i> Remove</button>
</div>

Comments

0

Try this

HTML

<div onclick="deleteGroup({{$value->id}})">
                            <i class="material-icons delete-icon">delete</i>
                        </div>

JavaScript

function deleteGroup($id) {
                if (confirm("Are you sure you want to delete this group?")) {
                    $.ajax({
                        type: "GET",
                        url: "/lecture/destroy/" + $id, //Your URL here
                        success: function (data) {
                            if (data.flag) {
                                alert(data.message);
                            } else {
                                alert(data.message);
                            }
                        },
                        error: function (response) {
                            console.log(response);
                        }
                    });



                }
            }

Controller

public function delete_group($id) {
        $grpDetails = Lecture::find($id);
        if (!empty($grpDetails)) {
            $isDelete = Lecture::find($id)->delete();
            if ($isDelete) {
                return response()->json([
                            'flag' => true,
                            'message' => 'Success'
                                ], 200);
            } else {
                return response()->json([
                            'flag' => false,
                            'message' => 'Something went wrong'
                ]);
            }
        } else {
            return response()->json([
                        'flag' => false,
                        'message' => 'Data Not Found'
            ]);
        }
    }

You can view image here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.