Updated my thread:
My models:
ModelPost (id,title,content,status)
ModelPostAnnotation(id,annotation,post_id)
My code:
Controller:
class PostController extends RController
{
public function actionChangestatus($id, $newstatus)
{
$modelPost=$this->loadModel($id);
$modelPost->status=$newstatus;
if($newstatus == 2){
$modelPostAnnotation=new PostAnnotation;
if(isset($_POST['PostAnnotation']))
{
$modelPostAnnotation->attributes=$_POST['PostAnnotation'];
if($modelPostAnnotation->save() && $modelPost->save())
{
echo CJSON::encode(array(
'status'=>'success',
'flash'=>"Annotation added and status changed",
));
exit;
}
}
echo CJSON::encode(array(
'status'=>'failure',
'div'=>$this->renderPartial('_form_annotations', array('modelPostAnnotations'=>$modelPostAnnotation), true)
));
exit;
}
else {
if($modelPost->save())
{
echo CJSON::encode(array(
'status'=>'success',
'flash'=>"Status changed",
));
exit;
}
}
}
View file
<?php
echo CHtml::ajaxLink(
'Change status to cancelled',
array('post/changestatus/', 'id'=>$model->id, 'newstatus'=>2),
array(
'type'=>'POST',
'data'=> "js:$(this).serialize()",
'dataType'=>'json',
'success'=>"function(data)
{
if (data.status == 'failure')
{
$('#dialog').dialog('open');
$('#dialog div.divForDialog').html(data.div);
}
else
{
$('#dialog').dialog('open');
$('#dialog div.divForDialog').html(data.flash);
setTimeout(\"$('#dialog').dialog('close') \",1500);
}
} ",
)
);
?>
<?php $this->beginWidget('zii.widgets.jui.CJuiDialog',
array(
'id'=>'dialog',
'options'=>array(
'title'=>'Annotations',
'autoOpen'=>false,
'modal'=>true,
'width'=>550,
'height'=>190,
)
));
?>
<div class="divForDialog"></div>
<?php $this->endWidget();?>
View (form_annotations.php)
<?php $form=$this->beginWidget('bootstrap.widgets.TbActiveForm', array(
'id'=>'annotations-form',
'type'=>'horizontal',
)); ?>
<?php echo $form->textFieldRow($modelPostAnnotation,'annotation'); ?>
<?php $this->widget('bootstrap.widgets.TbButton', array(
'buttonType'=>'submit',
'type'=>'primary',
'label'=>'Save',
)); ?>
<?php $this->endWidget(); ?>
When i change post status to for example 5 - everything is ok - status is changed and response is displayed in dialog. But when i want to change post status to 2 - response is not displayed in dialog, but as raw text:
This same, when i try change post status to 2 - i can save changed status and annotations, but response is not displayed in dialog - but as raw text:
What am i doing wrong?
Thank you for your time and helping
Greetings
Tom