Ajaxlink and flash message

Hi,

i’m trying to use flash message in an action called by an ajaxlink, but the flash message isn’t displayed.

I tried using the same action in in simple link and the message appears this time, so my action code seems correct.

Here is my grid:




<?php

$this->breadcrumbs=array(

	'Groups'=>array('index'),

	'Manage',

);


........


Yii::app()->clientScript->registerScript('search', "

$('.search-button').click(function(){

	$('.search-form').toggle();

	return false;

});

$('.search-form form').submit(function(){

	$.fn.yiiGridView.update('groups-grid', {

		data: $(this).serialize()

	});

	return false;

});

");

?>


<h1>Gestion des groupes</h1>




<!-- affichage des messages flash -->

<?php

    foreach(Yii::app()->user->getFlashes() as $key => $message) {

        echo '<div class="flash-' . $key . '">' . $message . "</div>\n";

    }

?>




.....




<!-- ajout bloc pour choix du cours par default (utilise par student/admin) -->

<br/>

<?php

echo CHtml::ajaxLink("Sélection du groupe par défaut",

$this->createUrl('creeGroupDefault'),

array(

"type" => "post",   // POST donc.

"data" => "js:{ids:$.fn.yiiGridView.getSelection('groups-grid')}",

'success'=> 'js:function(data, textStatus, XMLHttpRequest){'.

'if(data!=0)$.fn.yiiGridView.update("groups-grid")}'

));

?>





<!-- ajout pour ajax -->

<?php echo CHtml::beginForm('','post',array('id'=>'groups-form')); ?>




<?php $this->widget('zii.widgets.grid.CGridView', array(

	'id'=>'groups-grid',

	'dataProvider'=>$model->search(),

	'selectableRows'=>1, 

	'filter'=>$model,

	'columns'=>array(

       	array('header'=>'Défaut', 


.....


		),

	),

)); 


// ajout pour ajax 

echo CHtml::endForm();


?>



And my controller (which works):




// fonction permettant de sélectionner un groupe par defaut

	public function actionCreeGroupDefault()

	{


		Yii::import('ext.EUserFlash');


		if( Yii::app()->request->isPostRequest && isset($_POST['ids']) && is_array($_POST['ids']))

		{

			Yii::app()->user->setState('GroupDefault','');

			foreach ($_POST['ids'] as $modelId) // on lance une boucle meme si on a mis selectableRows a 1 (on ne doit donc avoir qu'une seule occurrence selectionnee)

			{

				Yii::app()->user->setState('GroupDefault',$modelId);

			}

		}

// message flash

		Yii::app()->user->setFlash('success', 'Choix du groupe par défaut: '.Yii::app()->user->getState('GroupDefault'));

		$this->redirect(array('groups/admin'));


	} 

// fin fonction creeGroupDefault




Does anyone can help me?

After severals tests, i’ve modified the controller and now didn’t redirect no more.

In that case, it almost works with the option "autoUpdateFlash" set to false, but i need to refresh the form for my flash message appears.

I add a div called target in my view to show the message, and add option “‘update’ => ‘#target’,” but it doesn’t work neither.

Did somenone meet that kind of problem?

Unless I’m wrong, you’re updating only the grid by Ajax, your PHP code displaying flash messages is correct, but it is only interpreted on server-side. I mean that it is not run upon an Ajax request.

If my explanation is correct, you have one simple solution, echo the flash in your controller action in an invisible div instead of setFlash, and update some other div upon your ajax success.

Thank you for the reply, but i don’t really understand the way to replace the setFlash.

This is my controller:




	public function actionCreeMessageDefault()

	{

		if( Yii::app()->request->isPostRequest && isset($_POST['ids']) && is_array($_POST['ids']))

		{

			Yii::app()->user->setState('MessageDefault','');

			foreach ($_POST['ids'] as $modelId) // on lance une boucle meme si on a mis selectableRows a 1 (on ne doit donc avoir qu'une seule occurrence selectionnee)

			{

				Yii::app()->user->setState('MessageDefault',$modelId);

			}

// message flash

			Yii::app()->user->setFlash('success', 'Choix du message par défaut: '.Yii::app()->user->getState('MessageDefault'));

//			$this->redirect(array('messageType/admin'));

		}

	} 



Can you tell me what you would return in this cas?

This is how I’d do it (example only, you need to adapt)

In the controller


public function actionCreeMessageDefault($id) {

    if (Yii::app()->request->isAjaxRequest) {

        …

        echo '<div class="flash-success">L\'action requise s\'est déroulée avec succès</div>';

    }

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

}

In the view




…

<div id="resultatAjax"></div>

…

<?php echo CHtml::ajaxLink('Sélection du groupe par défaut',

    $this->createUrl('creeMessageDefault'),

    array(

        'type' => 'post',

        'data' => "js:{ids:$.fn.yiiGridView.getSelection('groups-grid')}",

        'success' => "js:function(data, textStatus, XMLHttpRequest) {

                                if(data!='') {

                                    $.fn.yiiGridView.update('groups-grid');

                                    $('#resultatAjax').html(data);

                                }

                             }"

    )

); ?>

‘thanks, it works’ !!!

I’m sure your code will not help only me.

Have a nice day and weekend.