O
O
Oleg Lysenko2016-05-20 00:03:12
Yii
Oleg Lysenko, 2016-05-20 00:03:12

Problem with pjax?

Hello, the situation is this: when I click on the button, I need to send two post requests and update the content of a certain block. Without Pjax, requests are sent arbitrarily when clicked, but if with it, then Pjax update goes on every click, and requests go only after the first click, and that’s all, only after the page is refreshed. I have already broken my whole head, I don’t understand what the problem is, I ask for help, I am attaching the code

<?php Pjax::begin(); ?>
        <?php if($productsInCart):?>

            <table id="cart_products"cellspacing="0">
                <tr>
                    <th>Удалить</th>
                    <th>Наименование</th>
                    <th>Цена</th>
                    <th>Количество</th>
                    <th>Всего</th>
                </tr>

                <?php foreach($prods as $product):?>
                    <tr>
                        <td><a href="/cart/delete/<?php// echo $product['id'];?>" class="del">
                                <img src="/img/del_prod_pic.png">
                            </a></td>
                        <td><?= $product->name;?></td>
                        <td><?= $product['prices'][0]->price;?></td>
                        <td>
                            <?= $productsInCart[$product['id']];?> шт.
                            <a href="" class="to_cart" data-id="<?= $product->id;?>">
                                <img src="/img/plus.png">
                            </a>
                            <a href="#" class="cart_del" data-id="<?= $product->id;?>">
                                <img src="/img/minus.png">
                            </a>
                        </td>
                        <td><?= Order::getProdTotalPrice($product->id);?></td>
                    </tr>
                <?php endforeach;?>
                <tr id="total_sum">
                    <td></td>
                    <td></td>
                    <td></td>
                    <td>Итого в корзине: </td>
                    <td><?= $totalPrice;?></td>
                </tr>
            </table>

            <a href="/cart/checkout" class="checkout">
                Оформить заказ
            </a>

        <?php else:?>
            <h2 id="empty_cart">Ваша корзина пуста</h2>
            <a href="/" id="empty_cart_to_main">Продолжить покупки</a>
        <?php endif;?>
        <?= Html::a("Обновить", ['/cart'], ['class' => 'refresh btn btn-lg btn-primary']);?>
        <?php Pjax::end(); ?>

JS
$(document).ready(function(){
    $('.to_cart').click(function(){
        var id = $(this).attr("data-id");
        $.post("/toCart/"+id, {}, function(data){
            $('.cart_count span').html(data);
        });
        $.post("/updateCount/"+id, {}, function(data){
            $('.to_cart span#prod_'+id).html(data);
        });
        $('.refresh').click();
        return false;
    });
});

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
holfza, 2016-05-20
@oleglysenko

$('body').on('click', '.to_cart' , function(){
...
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question