Ajaxlink Not Working

I have made in layouts/main.php code to render cart as partial:




   $session = Yii::app()->session;

   $cart = Cart::GetCart($session['cart']);


    $this->renderPartial('//layouts/cart', array(

        'cart' => $cart,

    ), false, true );



Then the page is loaded I receive code 2 times in the bottom of the page and in rendered partial section #cart-content:

page bottom:




jQuery(function($) {

$('body').on('click','#yt1',function(){jQuery.ajax({'url':'en/index.php?r=cart/delete&id=0','cache':false,'success':function(html){jQuery("#cart-content").html(html)}});return false;});

$('body').on('click','#yt2',function(){jQuery.ajax({'url':'en/index.php?r=cart/delete&id=1','cache':false,'success':function(html){jQuery("#cart-content").html(html)}});return false;});

...

$('body').on('click','#yt8',function(){jQuery.ajax({'url':'en/index.php?r=cart/clear','cache':false,'success':function(html){jQuery("#cart-content").html(html)}});return false;});

});

/*]]>*/



from /layouts/cart.php:




<script type="text/javascript">

/*<![CDATA[*/

$('body').on('click','#yt1',function(){jQuery.ajax({'url':'en/index.php?r=cart/delete&id=0','cache':false,'success':function(html){jQuery("#cart-content").html(html)}});return false;});

$('body').on('click','#yt2',function(){jQuery.ajax({'url':'en/index.php?r=cart/delete&id=1','cache':false,'success':function(html){jQuery("#cart-content").html(html)}});return false;});

$('body').on('click','#yt3',function(){jQuery.ajax({'url':'en/index.php?r=cart/delete&id=2','cache':false,'success':function(html){jQuery("#cart-content").html(html)}});return false;});

$('body').on('click','#yt4',function(){jQuery.ajax({'url':'en/index.php?r=cart/delete&id=3','cache':false,'success':function(html){jQuery("#cart-content").html(html)}});return false;});

$('body').on('click','#yt5',function(){jQuery.ajax({'url':'en/index.php?r=cart/delete&id=4','cache':false,'success':function(html){jQuery("#cart-content").html(html)}});return false;});

$('body').on('click','#yt6',function(){jQuery.ajax({'url':'en/index.php?r=cart/delete&id=5','cache':false,'success':function(html){jQuery("#cart-content").html(html)}});return false;});

$('body').on('click','#yt7',function(){jQuery.ajax({'url':'en/index.php?r=cart/delete&id=6','cache':false,'success':function(html){jQuery("#cart-content").html(html)}});return false;});

/*]]>*/

</script>



Delete item from the cart is called mutiple times, so the two items deleted.

When the #cart-content is reloaded i get yt0 id for delete link which is stands for add to cart button id.

I have tryed several solutions but they either don’t work or delete miltiply times. I can use html options

to set uniqid() ids, but the links does not work, nothing deleted from cart.

Please give me a short path.

Thanks.

I have read:

http://www.yiiframework.com/wiki/87/ajax-update/

http://www.yiiframework.com/wiki/178/how-to-avoid-multiple-ajax-request/

http://www.yiiframework.com/forum/index.php/topic/31852-ajaxlink-does-not-work/

http://www.yiiframework.com/forum/index.php/topic/16950-chtmlajaxlink-refresh-problem/

and other

There was a similar problem in the blog demo application… check this issue for a possible solution and explanation - https://github.com/yiisoft/yii/issues/148

Is it possible to do it in my case like in blog demo?

I can see only links to actions like:

<a class="delete" title="Delete" href="/demos/blog/index.php/post/delete?id=8"><img src="/demos/blog/assets/81dfc6af/gridview/delete.png" alt="Delete" /></a>

So there is no ajax calls on page http://www.yiiframework.com/demos/blog/index.php/post/admin.

Also , I could not find anything valuable in blog demo source from yii 1.1.12 source. Where should I look?

As alternative solution I can generate jquery code and ajax links by hardcode, not using ajaxLink function.

I have tryed to put hardcoded links insted of generated by yii. I got an error when first call cart item is deleted and #cart-content is updated, on a next call to delete cart item link "delete" is not working. I can see in firebug that now ajax is executed two times. When I try to send request without ajax to delete php script I get cart item deleted and #cart-content content. Where is a problem? Please help. Thanks!

My links source code:




<a id="cartdelete-link-1" href="#">Delete</a>



My jquery source code:




<script type="text/javascript">

/*<![CDATA[*/

$('body').on('click','#cartdelete-link-0',function(){jQuery.ajax({'url':'index.php?r=cart/delete&id=0','cache':false,'success':function(html){jQuery("#cart-content").replaceWith(html)}});return false;});

$('body').on('click','#cartdelete-link-1',function(){jQuery.ajax({'url':'index.php?r=cart/delete&id=1','cache':false,'success':function(html){jQuery("#cart-content").replaceWith(html)}});return false;});

$('body').on('click','#cartdelete-link-2',function(){jQuery.ajax({'url':'index.php?r=cart/delete&id=2','cache':false,'success':function(html){jQuery("#cart-content").replaceWith(html)}});return false;});

/*]]>*/

</script>



There is some javascript error, becouse another ajax links are not working. Please advice any debugger to discover invalid code and error console.