1

Html:

<form id="myform" class="delete-photo col-md-12" method="POST" action="/admin/category/{{$catget->id}}">
        <input type="hidden" name="_method" value="delete">                    
            {{ csrf_field() }}                            
            <div class="form-group">
                <button type="submit" data-photo-id="{{$catget->id}}"
                class="submitdel btn btn-danger"
                >Delete Category</button>
            </div>                    
    </form>

Js:

<script type="text/javascript">
 $('.delete-photo').click(function(e) {
   e.preventDefault(); // Prevent the href from redirecting directly
   var linkURL = $(this).attr("action");
   warnBeforeRedirect(linkURL);
 });

 function warnBeforeRedirect(linkURL) {
   swal({
     title: "Leave this site?", 
     text: "If you click 'OK', you will be redirected to " + linkURL, 
     type: "warning",
     showCancelButton: true
   }, function() {
     // Redirect the user
     window.location.href = linkURL;
   });
 }
</script>

I am trying to create a confirmation button with sweetalert. I tryed to redirect to correct route which is (http://localhost:8000/admin/category/13) but the laravel says:

MethodNotAllowedHttpException in RouteCollection.php line 219:....

My route is:

Route::delete('/admin/category/{id}', 'adminpanel@deletecategory');

What can be possible wrong or fix that issue?

2
  • are you using laravel 5.1 Commented Mar 24, 2016 at 10:53
  • Yes I am using laravel 5.1 I have to add also without this javascipt code the route is working and button delete the category. Commented Mar 24, 2016 at 10:55

1 Answer 1

1

js:

 $('.delete-photo').click(function(e) {
   e.preventDefault(); // Prevent the href from redirecting directly
   var linkURL = $(this).attr("action");
   warnBeforeRedirect(linkURL);
 });

 function warnBeforeRedirect(linkURL) {
   swal({
        title: "Are you sure?",
        text: "You will not be able to recover this imaginary file!",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "Yes, delete it!",
        cancelButtonText: "No, cancel plx!",
        closeOnConfirm: false,
        closeOnCancel: false 
    },
    function(isConfirm) {
        if (isConfirm) {                        
            document.getElementById("myform").submit(); 
        } else {
            swal("Cancelled", "Your imaginary file is safe :)", "error");
        }
    }
);
 }

My problems was: 1) Submit must through the form to active the controller method delete. 2) The previus javascript didn't took the csrf token

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.