urreta17
urreta17

Reputation: 183

Symfony delete ajax

I have a problem when I want delete a register with Ajax and Symfony, in template Twig.

<tbody>
    {% for entity in entities %}
        <tr>
            <td>
                <a class="delete btn btn-danger btn-xs glyphicon glyphicon-trash" data-playgroup-id="{{ entity.id }}" ></a>
            </td>
        </tr>
    {% endfor %}
</tbody>

Ajax:

$(document).ready(function() {

    $(".delete").click(function(){
        var pid = $(this).attr("data-playgroup-id");

        bootbox.confirm("Are you sure?", function(result) {
            if(result){
                $.ajax({ 
                    url: '{{path('playergroup_delete', { 'id': pid}) }}',
                    type: 'delete', 
                    success: function(result) {
                        console.log('Delete');
                    },
                    error: function(e){
                        console.log(e.responseText);
                    }
                });
            }
        });
    });
});

I receive the next error:

Variable "pid" does not exists.

Thanks!

Upvotes: 0

Views: 2904

Answers (3)

Anas EL KORCHI
Anas EL KORCHI

Reputation: 2058

As MouradK say you ar passing a variable in a twig function (server side) and you are getting this variable using javascript (client side). to solve this do something like this :

$(document).ready(function() {

    $(".delete").click(function(){
        var pid = $(this).attr("data-playgroup-id");

        bootbox.confirm("Are you sure?", function(result) {
            url = '{{path('playergroup_delete', { 'id': 0}) }}';
            url = $url.replace("0",pid);
            if(result){
                $.ajax({ 
                    url: url,
                    type: 'delete', 
                    success: function(result) {
                        console.log('Delete');
                    },
                    error: function(e){
                        console.log(e.responseText);
                    }
                });
            }
        });
    });
});

Upvotes: 1

MouradK
MouradK

Reputation: 1567

The error is :

You are trying to set a variable comming from the client (javascript) in your twig template (which is a server side).

Upvotes: 0

Alessandro Lai
Alessandro Lai

Reputation: 2274

It means that you did not pass the pid variable to your Twig template.

Pass it trough the controller and you'll be fine

Upvotes: 0

Related Questions