Mrayhi Amine Mrayhi Amine - 3 years ago 744
Twig Question

using ajax in a symfony project to delete item

hello I'm looking for a script that could allow me to delete item in a symfony project using ajax, I didn't find any solution for this I tried to delete an item via id (I gave a static id) and then get the result without refreshing the page
here is my controller

$em = $this->getDoctrine()->getManager();
$evenement = $em->getRepository("TunisiaMallBundle:Evenement")->find(39);
$evenements = $em->getRepository("TunisiaMallBundle:Evenement")->findAll();
$response = new Response();
$response->headers->set('Content-Type', 'application/json');
return $response;

I didn't know what I'm I supposed to return too.

Answer Source

In your twig put something like this :

<button class="deleteBtn" id="{{ }}">Remove</button>

Here is the ajax using JQuery :


$('.deleteBtn').click(function (e) {

    itemId = $(this).attr('id');


        url: frm.attr('action'),
        data: {'entityId':itemId},
        method: 'post',
        success: function (data, reponse) {

         if(reponse == 'good' ){
            //appear pop to say success blabla
        error: function () {
            //appear pop to say error blabla


And the Controller :

public function ajaxDeleteItemAction(Request $request)

    $id = $request->get('entityId');
    $em = $this->getDoctrine()->getManager();
    $evenement = $em->getRepository("TunisiaMallBundle:Evenement")-


    return new JsonResponse('good');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download