Well, after taking a closer look at your code, I second rootbear in a sense.
I dont't think that your combatReport is correctly updated in your db.
The first time you click on the link, it will update the image. But can you reverse it to the original state by another click?
I guess that the ajax calls will fail to flip-flop the image in the 2nd call and thereafter.
echo CHtml::ajaxLink('<img id="'.$data['combatIdent'].'" src="/images/icn_share'.$data['shareFlag'].'.png">', 'ajaxRequest', array(
'type'=>'POST',
'data'=>array(
'crAction'=>'updateShareFlag',
'combatIdent'=>$data['combatIdent'],
'shareFlag'=>$data['shareFlag'],
),
'success'=>'function(data){
var source_image = "/images/icn_share"+data+".png";
$("#' . $data['combatIdent'] .'").attr("src",source_image);
}'
));
In the above, "src" attribute of img will be updated by ajax call, e.g. from "icn_share_no" to "icon_share_yes".
But the 'shareFlag' parameter of the ajax call will never be changed. If it was originally '_no', then it will remain '_no' forever.
And in your controller code:
public function actionAjaxRequest() {
// Switch CR share flag
if($_POST['crAction'] == 'updateShareFlag') {
$model = new combatReport();
$model->combatIdent = $_POST['combatIdent'];
$model->shareFlag = $_POST['shareFlag'];
$shareFlag = $model->switchShareFlag();
echo $shareFlag;
}
}
Probably you are flip-flopping the shareFlag and saving the model in switchShareFlag().
But $model->shareFlag always receives the same value that is initially set to the view.
And the way you update the AR model looks a little strange to me.
Why don't you load it from db using 'combatIdent', instead of creating a new instance?
$model = combatReport::model()->findByPk($_POST['combatIdent']);
$model->shareFlag = ($model->shareFlag === '_yes') ? '_no' : '_yes';
$model->save();
echo $model->shareFlag;
And by doing this, you will no longer need to include 'shareFlag' in your ajax call parameter.