I have a list that is populated and next to each item a remove button is generated. The remove button sends the current list of items and the key id that is to be deleted. It works and then it doesn’t. Sometimes it’ll delete the right item, sometimes it won’t delete the item and rather just changed the displayed item values and sometimes it’ll even add an item. Eventually if I click enough they’ll add disappear.
This is a simplified version of what I want to do, but the problem is still visible.
View:
<div id="display"></div>
<?php $items=array(1,2,3,4); ?>
<?php echo CHtml::ajaxButton('Add List',array('AddItem'),array('update'=>'#display','type'=>'POST','data'=> array('items'=>$items))); ?>
_list:
foreach ($items as $key=>$item)
{
echo $item . CHtml::ajaxButton('Remove',array('RemoveItem'),array('update'=>'#display','id'=>uniqid(),'type'=>'post','data'=>array('id'=>$key,'items'=>$items))) . '<br />';
}
Controller:
public function actionAddItem()
{
$data['items']=$_POST['items'];
$this->renderPartial('_list',$data,false,true);
}
public function actionRemoveItem()
{
$key=$_POST['id'];
$items=$_POST['items'];
unset($items[$key]);
$data['items']=$items;
$this->renderPartial('_list',$data,false,true);
}
I’ve read about generated ajax buttons and links on top of existing forms causing problems. I’ve tried all the solutions I could dig up, but it’s still not working consistently. I feel that all the data being passed is correct and the remove better is also correct, but it’s something with ajax I just don’t understand yet. How would you fix this problem? It’d be nice for users to be able to delete from a list using ajax. Thanks!