Question

I have a ajax link in yii when user click it do something and update a element html (content of td) without refresh page how can change 2 element html?

I explain more: in view i have a table by some td for show information a td for send information (there are a image ajaxlink) and a td by message of content (example: "not send")

When user click image link call a function and valid information (...) and change message of td ('validation is success').("not send" change to "validation is success") I did what I was told but i want to remove ajaxlink and replace it by a simple image how
can I do it?

i add:

CHtml::image(Yii::app()->theme->baseUrl . "/img/gridview/email-send.png", '', array('title' =>  'sended success'))

view.php:

<td id="send_<?php echo CHtml::encode($profileInformationServices->id); ?>"><?php echo "not send"; ?></td>
<td id="sended_<?php echo CHtml::encode($profileInformationServices->id); ?>">

      <?php                                        
            echo  $profileInformationServices->send ?  CHtml::image(Yii::app()->theme->baseUrl . "/img/gridview/email-send.png", '', array(                                                                'title' =>  'sended success')):
                      CHtml::ajaxLink(CHtml::image(Yii::app()->theme->baseUrl . "/img/gridview/send.png", '', array(
                                                            'title' => 'Send for valid')), Yii::app()->createUrl('/profileInformationService/send'),
                 array( // ajaxOptions
                     'type' => 'POST',
                        'data' => array( 'id' => $profileInformationServices->id ),
                           'update' => '#send_'.$profileInformationServices->id,
                        )

                      );

         ?>


 </td>

controller:

public function actionSend() {
   if (Yii::app()->request->isPostRequest) {
        $model = $this->loadModel($_POST["id"]);

        $model->send = 1;
        $model->save();
        echo "sended success";
    }
}
Was it helpful?

Solution

Use ajaxLink callback to change the text in td.

    <?php
    //I am keeping your ternary code in if-else condition for readability

    if($profileInformationServices->send)
    {
        echo CHtml::image(Yii::app()->theme->baseUrl . "/img/gridview/email-send.png", '', array('title' =>  'sended success'));
    }
    else
    {
        echo CHtml::ajaxLink
        (
            CHtml::image(Yii::app()->theme->baseUrl . "/img/gridview/send.png", '', array('title' => 'Send for valid')), 
            Yii::app()->createUrl('/profileInformationService/send'), 
            array
            (
                'type' => 'POST',
                'data' => array('id' => $profileInformationServices->id),                    
                'success'=>'function(data) 
                {
                     if(data=="sended success")
                     {
                        $("#send_'.$profileInformationServices->id.'").html("validation is success");
                     }
                }',
            )
        );
    }
    ?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top