0
votes

I'm building a website with Yii framework 1.1 and i'm implementing a portion wherein i have a like button associated with each post.i want to update the content of the like buttons text everytime i click on it without refreshing the page?please help?

EDIT i did this `id; $foo = $data->likes; echo CHtml::ajaxbutton($foo.' '.'Likes', array('post/like/'.$id), array( 'type'=>'POST', 'success'=>'js:function(data){ $.fn.yiiAjaxButton.update("label");}') );

?>` still doesnt work

1
Did you try something?!SaidbakR
Well, AJAX can solve this problem easily. Do you have any background knowledge on it?Snowwolf
@sємsєм i tried this thing called render and render partial,no luck now what it does it ,the like button is linked to a controller action and it updates it only when the page is refreshed.Rahul Mahadev

1 Answers

0
votes

Your View should be like bellow

    <?php
    $postId = 1; //Your post id
    echo CHtml::Button('SUBMIT', array('onclick' => 'getComments(this);', 'data-value' => $postId, 'value' => 'Get Comments'));
    ?> 

And Write your Ajax call some thing like

    <script type="text/javascript">
        function getComments(obj)
        {
            $PostID = $(obj).data('value');
            $.get('Controller/YourMethod', {id:$PostID}, function(dataJSON)
            {
                  //Get data in JSON formate
            },'JSON');
        }
    </script>

EDIT

If you want to add Ajax call directly to your button, you can do as

    <?php
    $postId = 1;
    echo CHtml::Button('SUBMIT', array('value' => 'Get Comments','onclick' => ''
        . '$.get("Controller/YourMethod", {id:'.$postId.'}, function(dataJSON)
            {
                  //Do what ever you want here
            },"JSON");'));
    ?>