I am still learning Laravel 4 so I have a resourceful controller tasks and I am having a problem implementing my jquery.
My delete works fine without jquery so i think that I am not providing the proper url
I have found one problem I don't know how to pass my id to jquery,it apears that is not assigned.

$('#delete').on("click",function(event) {
    event.preventDefault();
    var answer = confirm('Are you sure you want to delete this?');
if (answer===true)
{
    $.ajax({
    url: 'tasks/'+id,
    type: 'POSt',
    success: function(result) {
        // Do something with the result
    }
  });
}




else
{
  return false;
}
});

My view with the delete part

{{ Form::open(array('method'=>'DELETE', 'route'=>array('tasks.destroy', $t->id))) }}
                {{Form::submit('Delete',array('class'=>'btn btn-danger','id'=>'delete'))}}
                {{Form::close()}}

Thank you for your help

Dani AI

Generated

A concise, practical checklist and pattern that fixes the common pitfalls seen in this thread (notably using the form action, the duplicate id issue raised by , and ’s “try DELETE” hint).

Keep the flow simple

  • Give each delete control a class (not the same id for every item) and either a data-url attribute or rely on the enclosing form’s action. Duplicate ids on repeated rows will bite you.
  • Ensure the CSRF token is sent with the AJAX request (Laravel 4 will block the request otherwise).
  • Either send a real HTTP DELETE or send POST with a method override if intermediate proxies strip non-GET/POST verbs.

Example pieces to add to your layout and scripts

  • Add a small meta tag with the CSRF token in your master layout and use $.ajaxSetup to attach it to every request.
<meta name="csrf-token" content="<?php echo csrf_token(); ?>">

$.ajaxSetup({
  headers: { 'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content') }
});
  • Use delegated event handling, read the URL from a data attribute (or closest form), confirm, then send the request and handle errors cleanly.
$(document).on('click', '.btn-delete', function(e){
  e.preventDefault();
  if (!confirm('Delete this?')) return;
  var url = $(this).data('url') || $(this).closest('form').attr('action');
  $.ajax({ url: url, type: 'DELETE',
    success: function(){ /* remove row */ },
    error: function(xhr){ console.error(xhr.status + ' ' + xhr.responseText); }
  });
});

Troubleshooting tips

  • If you get 500, check app/storage/logs/laravel.log for the actual exception. Use the browser Network tab to inspect request headers/body (verify token and method). Clear browser cache if responses look stale. If DELETE requests fail in your environment, send POST with a method override (_method=DELETE) instead.

Recommended Answers

All 9 Replies

Member Avatar for Member #120589
 url: 'tasks/'+id,

From where are you getting the 'id'?

That is my problem I don't know how to get it because I am using it directly in the route.
So I can't do this .

This is what it should go to my

url:route'=>array('tasks.destroy', $t->id

But i don't know how to make it work

Member Avatar for Member #120589

You seem to be mixing up your php and js vars.

I know that I can't mix php with jquery.I want to find a way to get the action form the delete form to my jquery.
For an input I would do it like this.

$('input').val();

I will dig deeper to see if I can find what i am looking for

Thanks for the reply.

Ok I found out that I can use the attribute method from jquery.

Now I have the delete working but i get 500 error form the server

var del = $('form').attr('action');
data={_method: 'delete' };

    $.ajax({
    url:  del,
    type: 'POSt',
    data:data,
    success: function(result) {
        console.log('You have deleted me.');
    }
  });

The error

DELETE  500 (Internal Server Error)    jquery-1.10.2.min.js:6
send jquery-1.10.2.min.js:6
x.extend.ajax jquery-1.10.2.min.js:6
(anonymous function) main.js:17
x.event.dispatch jquery-1.10.2.min.js:5
v.handle jquery-1.10.2.min.js:5

Ok it is working now I had a problem with my cache form the browser Thank you everyone.

Have you tried to change the type, in the ajax call, to DELETE?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.