Q:

Popup to other form in Yii

I am a beginner in Yii framework. I wanted to have a popup in my createform that links from my other table. The purpose of this is I have a dropbox, If there is no data that is I wanted to pick. I will create first. I want to have a popupbox in my table 1 create.php that will get the create form in my table 2. here is what I started. what seems to be the problem here?
here is my codes for table1 create.php
<?php echo CHtml::link('New Day',"",
array(
'style'=>'cursor:pointer; text-decoration:underline;',
'onClick'=>"{doDay(); $('#dialogDay).dialog('open');}"));?>
<?php
$this->beginWidget('zii.Widgets.jui.CJuiDialog',array(
'id'=>'dialogDay',
'options'=>array(
    'title'=>'Add New Day',
    'autoOpen'=>false,
    'modal'=>true,
    'width'=>550,
    'height'=>470,
),
));
?>
<div class="divForForm"></div>
<?php $this->endWidget();?>

<script type="text/javascript">
function doDay()
{
    <?php echo CHtml::ajax(array(
        'url'=>array('day/NewDay'),
        'data'=>"js:$(this).serialize()",
        'type'=>'post',
        'dataType'=>'json',
        'success'=>"function(data)
        {
        if(data.status == 'failure')
        {
            $('#dialogDay div.divForForm').html(data.div);
            $('#dialogDay div.divForForm form').submit(doDay);
        }
        else
        {
        window.location.href = ".Yii::app()->getBaseUrl().";
        }
        }",
))?>
    return false;

}

</script>

in my table 2 controller
public function actionNewDay()
{

    $model= new Day;
    if(isset($_POST['ajax']) && $_POST['ajax']=='day-form')
    {
        $model->attributes=$_POST['Day'];
        echo CActiveForm::validate($model);
        Yii::app()->end();
    }
    if(isset($_POST['Day']))
    {
        $model->attributes=$_POST['Day'];
        $name=$model->name;
        $mon=$model->mon;
        $tue=$model->tue;
        $wed=$model->wed;
        $thurs=$model->thurs;
        $fri=$model->fri;
        $sat=$model->sat;
        $sun=$model->sun;

        $dayassign = new DayAssign();
        if($model->save())
        {
            $dayassign->varName = $name;
            $dayassign->varMon = $mon;
            $dayassign->varTue = $tue;
            $dayassign->varWed = $wed;
            $dayassign->varThurs = $thurs;
            $dayassign->varFri = $fri;
            $dayassign->varSat = $sat;
            $dayassign->varSun = $sun;
            if($dayassign->save())
            {
                if(Yii::app()->request->isAjaxRequest)
                {
                    echo CJSON::encode(array(
                        'status'=>'success',
                    ));
                }
                else{
                    $url = Yii::app()->getBaseUrl();
                    Yii::app()->getRequest()->redirect($url);
                }
            }
        }
        if(Yii::app()->request->isAjaxRequest)
        {
            echo CJSON::encode(array(
                'status'=>'failure',
                'div'=>$this->renderPartial('day',array('model'=>$model),true)));
            exit;

        }
        else
            $this->render('day',array('model'=>$model,));
    }
}

A:

Did it by myself. here is the codes.
Scenario
I have a day model which contains shift eg. Night,Morning that is selected by my EmpSched model. Lets assume that the EmpSched wants to choose the Afternoon Shift but the day model doesnt have afternoon model yet, He will have to create a day first and lose the already inserted input.
We want to allow the user to create the day from the form of the empsche, without changing pages
This is what I done.
in my Day Controller:
    public function actionCreate()
{
    $model=new Day;

    // Uncomment the following line if AJAX validation is needed
    //$this->performAjaxValidation($model);

    if(isset($_POST['Day']))
    {
        $model->attributes=$_POST['Day'];
        if($model->save())
        {
            if(Yii::app()->request->isAjaxRequest)
            {
                echo CJSON::encode(array(
                   'status'=>'success',
                    'div'=>"Day successfully added"
                ));
                exit;
            }
            else
                $this->redirect(array('view','id'=>$model->id_day));
        }

    }
    if(Yii::app()->request->isAjaxRequest)
    {
        echo CJSON::encode(array(
            'status'=>'failure',
            'div'=>$this->renderPartial('_form',array('model'=>$model),true)));
        exit;
    }
    $this->render('create',array(
        'model'=>$model,
    ));

}

and in my EmpSched _form.php:
</div>
<?php echo CHtml::link('Create day', "",  // the link for open the dialog
array(
    'style'=>'cursor: pointer; text-decoration: underline;',
    'onclick'=>"{addDay(); $('#dialogDay').dialog('open');}"));?>

<?php
$this->beginWidget('zii.widgets.jui.CJuiDialog', array( // the dialog
'id'=>'dialogDay',
'options'=>array(
    'title'=>'Create Day',
    'autoOpen'=>false,
    'modal'=>true,
    'width'=>550,
    'height'=>470,
),
));?>
<div class="divForForm"></div>

<?php $this->endWidget();?>

<script type="text/javascript">
// here is the magic
function addDay()
{
    <?php echo CHtml::ajax(array(
            'url'=>array('day/create'),
            'data'=> "js:$(this).serialize()",
            'type'=>'post',
            'dataType'=>'json',
            'success'=>"function(data)
            {
                if (data.status == 'failure')
                {
                    $('#dialogDay div.divForForm').html(data.div);
                          // Here is the trick: on submit-> once again this function!
                    $('#dialogDay div.divForForm form').submit(addDay);
                }
                else
                {
                    $('#dialogDay div.divForForm').html(data.div);
                    setTimeout(\"$('#dialogDay').dialog('close') \",3000);
                }

            } ",
            ))?>
    return false;

}

</script>

I hope ill help someone who is on my own boat.

