Hi i’m using 2 AjaxLinks to perform actionAddToProject and actionDelFromProject.
The objective is add and remove links to a project from a list of links.
My LinkController:
public function actionIndex()
{
$dataProvider=new CActiveDataProvider('Link');
$this->render('index',array(
'dataProvider'=>$dataProvider,
'id_project'=>$id_project,
),false,true);
}
public function actionAddToProject()
{
if(isset($_POST['id_link'],$_REQUEST['id_project']))
{
//save links to project
$id_project = $_REQUEST['id_project'];
$id_link = $_POST['id_link'];
$modelprojectfile=new ProjectFile;
$modelprojectfile->deleteAll('id_project = :project AND id_link = :link',array(':project'=>$id_project,':link'=>$id_link));
$modelprojectfile->id_project=$id_project;
$modelprojectfile->id_link=$id_link;
$modelprojectfile->save();
echo "Link ".$id_link." added with success to project ".$id_project;
}
$dataProvider=new CActiveDataProvider('Link');
$this->renderPartial('index',array(
'dataProvider'=>$dataProvider,
'id_project'=>$id_project,
),false,true);
}
public function actionDelFromProject()
{
if(isset($_POST['id_link'],$_REQUEST['id_project']))
{
//remove links from project
$id_project = $_REQUEST['id_project'];
$id_link = $_POST['id_link'];
$modelprojectfile=new ProjectFile;
$modelprojectfile->deleteAll('id_project = :project AND id_link = :link',array(':project'=>$id_project,':link'=>$id_link));
echo "Link ".$id_link." deleted with success from project ".$id_project;
}
$dataProvider=new CActiveDataProvider('Link');
$this->renderPartial('index',array(
'dataProvider'=>$dataProvider,
'id_project'=>$id_project,
),false,true);
}
On my index _view i got:
<?php
$verifyLinkProject=ProjectLink::model()->findByAttributes(
array('id_link'=>CHtml::encode($data->id_link),
'id_project'=>CHtml::encode($_REQUEST['id_project']))
);
?>
<?php
if(count($verifyLinkProject)>0)
echo CHtml::ajaxLink(
'Delete from project',
array('link/DelFromProject'),
array('update' => '#index','type'=>'POST', 'data'=>array('id_link'=>CHtml::encode($data->id_link),
'id_project'=>CHtml::encode($_REQUEST['id_project'])))
);
else if (ctype_digit($_REQUEST['id_project']))
echo CHtml::ajaxLink(
'Add to project',
array('link/AddToProject'),
array('update' => '#index','type'=>'POST', 'data'=>array('id_link'=>CHtml::encode($data->id_link),
'id_project'=>CHtml::encode($_REQUEST['id_project'])))
);
?>
Now the strange thing is that this works… but when i click for example: ‘Delete from project’ it deletes the link and changes the ajaxlink to “Add to project” now its ok… but if i click again “Add to project” it will repeat the actions till that moment… if i clicked 5 times on that button next time i click it will first repeat all my clicks and just then will be performed my last action… confusing!!!