Changing partial view through radio buttons

I have reached a roadblock and hope one of the YII experts can help.

2 JUI Radio Buttons (Button Set) on a view are supposed to switch out a partial view. I was hoping I could just call a controller action when one of the radio buttons is clicked to achieve this. The action is supposed to re-render the view with a different partial view.

This is all happening on the logon form (see attachment). I want to allow people to logon or to sign up. logon needs less fields than sign up, also a different model (LogonForm versus User).

Is this possible?

Here is the code:

<?php

$this->beginWidget(‘zii.widgets.jui.CJuiButton’, array(

‘buttonType’=>‘buttonset’,

‘name’=>‘my-set’,

‘themeUrl’=> Yii::app()->theme->baseUrl . ‘/jquery-ui-1.8.22.custom/css’ ,

‘theme’=>‘buttonset’,

));

$this->widget(‘zii.widgets.jui.CJuiButton’, array(

‘name’=>‘modeRadio’,

‘id’=>‘radio1’,

‘caption’=>‘sign up’,

‘buttonType’=>‘radio’,

‘value’=>array(‘checked’=>‘checked’) ,

‘htmlOptions’=>array(‘value’=>‘SIGNUP’),

‘onclick’=>‘js:function(){$.ajax({type: “POST”,url: “http://localhost/yii/TeamDynamics/index.php?r=site/switch”}); }’,

));

$this->widget(‘zii.widgets.jui.CJuiButton’, array(

‘name’=>‘modeRadio’,

‘id’=>‘radio2’,

‘caption’=>‘login’,

‘buttonType’=>‘radio’,

‘htmlOptions’=>array(‘value’=>‘LOGIN’),

‘onclick’=>‘js:function(){$.ajax({type: “POST”,url: “http://localhost/yii/TeamDynamics/index.php?r=site/switch”}); }’,

));

$this->endWidget();

?>

Thank you,

Dieter

It is possible (although I never used the JUIButtons).

1.) I would create a separate action for signup and login as opposed to site/switch

2.) Check if the request to an action is done via ajax in your actions and only partially render the view when this is the case


If(Yii::app()->request->isAjaxRequest)

{

    $this->renderPartial('signup');

    //use $this->renderPartial('signup',$data,false,true); when your view contains js that has to be rendered too (fourth parameter => true)

    Yii::app()->end(); //As this is an ajax request you can end the app here

}

else

    $this->render('signup');

3.) You have to define a success callback in your js code to handle the response (the rendered partial). I wouldn’t use a POST as you aren’t posting anything yet. You only want to render the appropriate view I guess so a GET would be better. The rendered partial will then be placed into an element with id #container.




'js:function(){

    $.ajax({

        type: "GET",

        url: "'.$this->createUrl('site/signup').'"

        success : function(data) {

            $("#container").html(data);

        }

     });

}';

Hope it helps

Hannes

Hannes,

Thank you so much. This helped me get past my roadblock.

Beste Gruesse,

Dieter