从Controller Action传递和获取值到Ajax Success - Yii Framework

时间:2014-01-09 11:50:29

标签: javascript php ajax yii

我是yii Framework的新手现在我想将值从Controller动作传递给Ajax on Success,并获取我将它存储在php变量中的值。怎么做到这一点?

我的控制器操作:

public function actionNewrefList($id)
    {
        $model=new RefListModel();
        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);

        if(isset($_POST['RefListModel']))
        {
            $model->attributes=$_POST['RefListModel'];
            $model->crm_base_contact_id=$id;
            if($model->save())
            {
                /** Here i need to send value of $model->crm_ref_list_id to Ajax Onsuccess **/
                return;
            }
        }


    }

我的观点Ajax:

echo CHTML::ajaxSubmitButton('Save', Yii::app()->createUrl('baseContact/NewrefList',array("id" => $base)),
           array('success' => 'js:function(){
    '))

这里我必须从控制器传递值并将其存储在PHP变量中。

1 个答案:

答案 0 :(得分:2)

 if(isset($_POST['RefListModel']))
    {
        $model->attributes=$_POST['RefListModel'];
        $model->crm_base_contact_id=$id;
        if($model->save())
        {
            echo $model->crm_ref_list_id;
            Yii::app()->end();
        }
    }

在视图中:

echo CHTML::ajaxSubmitButton('Save', 
Yii::app()->createUrl('baseContact/NewrefList',
array("id" => $base)),
'success' => 'js:function(data){
    alert(data);
}'))