Yii Framework Forum: [Resolved] Stuck with ajaxLink - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

[Resolved] Stuck with ajaxLink I just don't get the point... Rate Topic: -----

#1 User is offline   Professor Hase 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 14-April 12

Posted 17 April 2012 - 08:27 PM

I'm trying to update an image via ajaxLink. The image has a unique css id and the function does return a valid response. I tried to work with the update property but I just don't get it working. So here are my snippets:

My View (part):
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'],
        ),
    ));


My Controller (part):
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;
            }
        }


I bet it's quite easy but I just don't get it ;)
0

#2 User is offline   rootbear 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 222
  • Joined: 17-June 11

Posted 17 April 2012 - 09:56 PM

View PostProfessor Hase, on 17 April 2012 - 08:27 PM, said:

I'm trying to update an image via ajaxLink. The image has a unique css id and the function does return a valid response. I tried to work with the update property but I just don't get it working. So here are my snippets:

My View (part):
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'],
        ),
    ));


My Controller (part):
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;
            }
        }


I bet it's quite easy but I just don't get it ;)


miss this?
$model->save();

I enjoy the Yii.sy coding life here.
0

#3 User is offline   Professor Hase 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 14-April 12

Posted 17 April 2012 - 10:10 PM

View Postrootbear, on 17 April 2012 - 09:56 PM, said:

miss this?
$model->save();



Nope. The model is working and returns a valid value. My problem here is that I don't get it how to update the IMG-Tag.

Example:
  • Image is <img src="/images/share_yes.png">
  • ajaxAction returns "no"
  • Image should get updated with <img src="/images/share_no.png">

0

#4 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,063
  • Joined: 16-February 11
  • Location:Japan

Posted 17 April 2012 - 11:32 PM

Your code doesn't receive the ajax response to update the html.
Try it for a test:
echo '<div id="test">foo bar</div>';
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'],
        ),
        'update'=>'#test', // look at this
    ));

I hope it will update "foo bar" to "no".
You have to replace the "src" attribute of the img tag instead. It should be possible, but might not be so simple and easy.
0

#5 User is offline   xent 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 97
  • Joined: 14-November 10

Posted 17 April 2012 - 11:56 PM

you might forget 'success' in ajax rensponse

'success'=>'function(data){
    var source_image = "/images/icn_share"+data;
    $("#id_of_image").attr("src",source_image);
}';

xent

My first app: School Information System
Find error? Post here
My Blog
0

#6 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,063
  • Joined: 16-February 11
  • Location:Japan

Posted 18 April 2012 - 12:51 AM

View Postxent, on 17 April 2012 - 11:56 PM, said:

'success'=>'function(data){
    var source_image = "/images/icn_share"+data;
    $("#id_of_image").attr("src",source_image);
}';



Yeah, that's it.
Or rather:
'success'=>'function(data){
    var source_image = "/images/icn_share"+data+".png";
    $("#' . $data['combatIdent'] .'").attr("src",source_image);
}';

0

#7 User is offline   rootbear 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 222
  • Joined: 17-June 11

Posted 18 April 2012 - 06:38 AM

View PostProfessor Hase, on 17 April 2012 - 10:10 PM, said:

Nope. The model is working and returns a valid value. My problem here is that I don't get it how to update the IMG-Tag.

Example:
  • Image is <img src="/images/share_yes.png">
  • ajaxAction returns "no"
  • Image should get updated with <img src="/images/share_no.png">



just wondering, if just fliping image to yes/no without $model->save(); why bother to use ajax? front-end js would do the trick.
I enjoy the Yii.sy coding life here.
0

#8 User is offline   Professor Hase 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 14-April 12

Posted 18 April 2012 - 10:08 AM

View Postsoftark, on 18 April 2012 - 12:51 AM, said:

Yeah, that's it.
Or rather:
'success'=>'function(data){
    var source_image = "/images/icn_share"+data+".png";
    $("#' . $data['combatIdent'] .'").attr("src",source_image);
}';



That it was. Now I know that this job completely relies on JS (which I don't like btw) :)
Thanks!
0

#9 User is offline   Professor Hase 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 14-April 12

Posted 18 April 2012 - 10:09 AM

View Postrootbear, on 18 April 2012 - 06:38 AM, said:

just wondering, if just fliping image to yes/no without $model->save(); why bother to use ajax? front-end js would do the trick.


Oh... There's still a world without AR out there ;)
0

#10 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,063
  • Joined: 16-February 11
  • Location:Japan

Posted 18 April 2012 - 11:37 AM

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? :unsure:

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. :)
0

#11 User is offline   Professor Hase 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 14-April 12

Posted 18 April 2012 - 08:05 PM

Maybe I forgot to mention that 'shareFlag'=>$data['shareFlag'] isn't used anymore and has been removed. I don't trust POST data in general so the model just get the call to switch to the next of 3 states and returns the state it was set to. This way it's working really fine.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users