How to do ajaxlink/ajax button for this.....not works....

Dear all,

I have the following stuff to let the user "bookmark a page" or "delete the bookmark" to a news page( in notescontroller).

Codes in NotesController… the bookmark url is something like this:


http://www.123.com/notes/save/420


      public function actionSave($id)

     {

$assign=NotesUserAssignment::model()->findByAttributes(array('notes_id'=>$this->loadModel($id)->id,'user_id'=>Yii::app()->user->id));

if(!$assign){

	        $model=new NotesUserAssignment;

                $notes=$this->loadModel($id);

                $model->notes_id = $notes->id;

                $model->user_id=Yii::app()->user->id;

                if($model->save())

                {

Yii::app()->user->setFlash('notes', "You have successfully bookmarked this notes.");

	        $this->redirect(array('view','id'=>$notes->id));

		}


           }

else { 

         throw new CHttpException(400,'Invalid request. You have already bookmarked this note. Please do not repeat this request again.');

              }


	}

For the view:


$this->widget('bootstrap.widgets.BootButton', array(

    'label'=>'Bookmark it',

     'icon'=>'inbox white',

     'url'=>'save/'.$model->id,

    'type'=>'primary',

    'htmlOptions'=>array('data-placement'=>'left','data-title'=>'Bookmark this note', 'data-content'=>'Once you bookmarked the note to your bookmarks, you can access it directly from your profile page.', 'rel'=>'popover'),

)); 

Is there a way I can do ajaxlink for this…?

I tried


echo CHtml::ajaxLink(

  "Link Text",

  Yii::app()->createUrl('save/'.$model->id),

  array( // ajaxOptions


    'beforeSend' => "function( request )

                     {

   //what should i put here....

                     }",

    'success' => "function( data )

                  {

     //...what should i put here

                  }",

   // 'data' => array( 'something here?' )  and this..?

  ),

  array( //htmlOptions

    'href' => Yii::app()->createUrl('save/'.$model->id ),

    'class' => $someclass,

  )

);

I am not very familiar with Yii’s ajax stuff…

Thanks so much for your help!!!!!!!

Thanks again! :D :D :D :D

Use ‘update’ in ajaxLink to call the action save, and use renderPartial to render the responde without layout.

Happy coding.

Thanks for your reply dude.

So I do something like this in the view?


<?php echo CHtml::ajaxLink('clickMe', array('save'), array('update'=>'#forAjaxRefresh'));?>

or


<?php echo CHtml::ajaxLink('clickMe', array('save/'.$model->id), array('update'=>'#forAjaxRefresh'));?>

Because actionSave($id) is with ($id)…

I know I need to render partial to make the "flash" work but now even the actions in the controller is not working…(not save to the database at all if i use ajax)

Thanks so much for your help!!!

Have a good day!

Tried another way…no works… :( ??? ???

Thanks for any help!!!

As a matter of fact, this syntax could work:


<?php echo CHtml::ajaxLink('clickMe', array('save/'.$model->id), array('update'=>'#forAjaxRefresh'));?>

But to be on the safe side


<?php echo CHtml::ajaxLink('clickMe', $this->createUrl('save', array('id' => $model->id)), array('update'=>'#forAjaxRefresh'));?>

Thanks for your help…I tried but it’s so weird…

I have an ajax button to let the user vote…after you vote, the button will be updated to another ajax button and you can revoke your vote…

Now I can use ajax to change to button from “vote for it " to " revoke the vote”. But then, I can’t change it back…Which means I can only request AJAX once!.. (Or I can only change the button form revoke to vote for it… still can only ajax once)

From the controller:


$this->renderPartial('_votedown',array('id'=>$notes->id));

From the View:


$game=GameVotes::model()->findByAttributes(array('notes_id'=>$model->id,'user_id'=>Yii::app()->user->id));


if(!$game){

echo CHtml::ajaxLink('Vote for it', $this->createUrl('voteup', array('id' => $model->id)), array('update'=>'#ajaxgame','type' => 'POST'), array('class'=>'btn btn-primary btn-success'));

}

else{

echo CHtml::ajaxLink('Voted', $this->createUrl('votedown', array('id' => $model->id)), array('update'=>'#ajaxgame','type' => 'POST'), array('class'=>'btn btn-primary btn-success'));

}

}

Finally…the _view…for ajax…


<?php

echo CHtml::ajaxLink('Voted', $this->createUrl('votedown', array('id' => $id)), array('update'=>'#ajaxgame','type' => 'POST'), array('class'=>'btn btn-primary btn-success'));

?>

and


<?php

echo CHtml::ajaxLink('Vote for it', $this->createUrl('voteup', array('id' => $id)), array('update'=>'#ajaxgame','type' => 'POST'), array('class'=>'btn btn-primary btn-success'));

?>

…Thanks!!