I am trying to pass a variable from an ajaxLink into my controller but my controller is not getting the variable.
//view
$ids = Yii::app()->storedData->getIds();
foreach($ids as $id) {
echo 'ID '.$id .'<br />';
echo CHtml::ajaxLink(
'remove',
array('/storedInfo/remove'),
array(
'data' => array('removeItem' => $id),
));
}
//controller
public function actionRemove() {
var_dump($_GET['removeItem']); // RETURNS string(0) ""
die();
}
The ajax array you’re passing to CHtml::ajaxLink ends up taking the whole array and passing it to CJavaScript::encode().
As you mentioned in your comments, the
$idyou were using to build your data array was a PHP object. When this got to CJavaScript::encode, things got wonky, and the results weren’t what you were expecting. Extracting a string or numeric value from $id, rather that passing the whole object should take care of this, e.g.:Or maybe something like this depending on the data structure: