1

I have the following code in my view:

@foreach ($articlestore as $art)

<form action="{{URL::to('/story/' . $art->id)}}" method="post" id="article_one">

<div class="form-group">
<input type="hidden" name="chosen_title" value="{{$art->id}}" class="form-control">
</div>
<button type ="button" onclick="deleteArticle({{ $art->id }})" id="Reco">Delete</button>
{{Form::token()}}

</form> 
@endforeach


<script type="text/javascript">

function deleteArticle(id) {

 $.ajax({
 url: '/Project/public/story/'+id,
 data: { "_token": "{{ csrf_token() }}" },
 type: 'DELETE',
 success: function(result) {
 console.log(result);
}
});
}

Controller:

public function destroy($id)
{

     $articledel = FootballArticle::find($id);
             $articledel->delete();


}

Im very new to AJAX and so im struggling to know how to delete a row from my table using JQuery. Any help would be much appreciated.

2
  • What problem are you having? What do you want it to do, and what is it doing? Commented Apr 30, 2015 at 2:25
  • Im simply trying to delete an article from my table using Ajax and with no success. Commented Apr 30, 2015 at 2:28

1 Answer 1

3

Change

<button type ="button" onclick="deleteArticle($art->id)" id="Reco">Delete</button>

to

<button type ="button" onclick="deleteArticle({{ $art->id }})" id="Reco">Delete</button>
                                              ^^          ^^

In deleteArticle() change

url: 'http://localhost/Project/public/story'+id,

to

url: '/Project/public/story/'+ id,
     ^^                   ^^

and add

data: { "_token": "{{ csrf_token() }}" },

so it looks like

$.ajax({
    url: '/project/public/story/' + id,
    data: { "_token": "{{ csrf_token() }}" },
    type: 'DELETE',
    success: function(result) {
        console.log(result);
    }
});

Also you don't need to do a redirect in your destroy() method when you make an ajax call

return Redirect::back();
Sign up to request clarification or add additional context in comments.

2 Comments

it's still not deleting. I noticed you missed out the id parameter in the deleteArticle() function. Do I need it? anyway i've tried with and without
i followed your method, it's working fine. It seems like the page doesn't refresh. return redirect(); doesn't work properly.

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.