How To Use Ajax When Clicking A Dropdown

i want my notification to be set 0 when the dropdown is clicked. after clicking dropdown, the value in <span id="notif"> must also change into 0.

script below is my progress so far. debugging by using chrome, i found error message "Uncought SyntaxError : Unexpected Token }".

please help if any of you know how to solve this problem. thank you :)

this view inside theme folder





<li class="dropdown" style="text-align: left" 

    onclick="   <?php

                    echo CHtml::ajax(array(

                        'id' =>'updatestatus',

                        'type' => 'POST',

                        'url' => Yii::app()->createUrl('tx/notification/updatestatus'),

                        'update' => '#notif',

                    ));        

                ?>"

>              

    <a href="#" data-toggle="dropdown" class="dropdown-toggle"  >

        <span class="glyphicon glyphicon-bell"></span> 

        <span id="notif" class="badge">

            <?php

            echo $unreadNotif;

            ?>

        </span> 

        <b class="caret"></b>

    </a>

    ...

    ...

</li>



notification controller




...

...


        public function filters() { 

            return array( 'rights', ); 


        } 

        public function allowedActions() { 

            return 'UpdateStatus';    

        } 




        public function actionUpdateStatus(){

            if(isset($_POST['updatestatus'])){

                $notifications = Notification::model()->findAllByAttributes(array('read_status'=>false,'notify_to'=>Yii::app()->user->id));

                foreach ($notifications as $notify) {

                    $notify->read_status=1;

                    $notify->save();

                }                  

                echo '0';     

                Yii::app()->end();

            }

        }

...

...



I tried your code above and followed your steps meticulously but it seems there is something wrong.Have you tested your code?

It’s a jquery error. for more help please try this - http://www.yiiframework.com/wiki/48/by-example-chtml#hh1

Also may be this will help you - http://www.yiiframework.com/wiki/739/how-to-display-dependent-drop-down-value-using-ajax-call-with-dynamically/

try this


<?php   Yii::app()->clientScript->registerScript(

      "test",

      "jQuery.ajax({

            type: 'POST',

            url: '".$url."',

            success: function(html){

               jQuery('#yourID').html(html);

        }

          });

      ",

      CClientScript::POS_READY

  ); ?>

actually my code was correct, but since i put the ajax inside html tag, the JQuery generated by Yii got mixed with the quatation mark ( " " ) of html inside onclick. with a little trick, the code is working fine now.





             	$ajax = CHtml::ajax(array(

                                        'type' => 'POST',

                                        'url' => Yii::app()->createUrl('tx/notification/updatestatus'),

                                        'update' => '#notif'

                                    ));  

                $ajax = str_replace('"', "'", $ajax);


		<li class="dropdown" style="text-align: left" onclick ="<?php echo $ajax; ?>" >



thank you for all your answer. :)