在Yii1中使用Ajax提交表单数据

时间:2015-10-13 04:47:31

标签: ajax yii1.x

我尝试使用ajax提交表单。以下是我的代码:

控制器代码:

public function actionIndex($complaint, $work)
    {
        ...
        $this->render('index',array(
            'model'         =>  $model,
            'work_order'    =>  $work_order,
            'work'          =>  $work,
            'complaint'     =>  $complaint,
            'work_complaint'=>  $work_complaint
        ));
    }

ajax行动

public function actionCreate($complaint,$work)
    {
    ...

        $this->renderPartial('create',array(
            'model'             =>  $model,
            'complaint'         =>  $complaint,
            'work'              =>  $work,
            'work_complaint'    =>  $work_complaint,
            'work_order'        =>  $work_order,
            'man_hour'          =>  $man_hour,
            'jobs'              =>  $jobs,
        ));
    }

我的观点

Create.php

<h1>Add Job</h1>

<?php $this->renderPartial('_form', array('model'           =>  $model,
                                          'work_complaint'  =>  $work_complaint,
                                          'work_order'      =>  $work_order,        
                                          'man_hour'        =>  $man_hour,      
                                          'jobs'            =>  $jobs,
                                          'complaint'       => $complaint, 
                                          'work'            => $work
                        )); ?>

my _form.php

<div class="form">



<?php $form=$this->beginWidget('CActiveForm', array(
            'id'=>'post-form',
            'enableAjaxValidation'=>false,
    )); ?>
...

    <div class="row buttons">
        <div class="col-md-6 col-lg-6" >
        <?php echo CHtml::ajaxSubmitButton ("Post",
                array('complaintJob/create','complaint'=>$complaint,'work'=>$work),
                            array('update' => '#post')); ?>
        </div>
    </div>



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

</div><!-- form -->

我的index.php

<div id="post">

<?php 
        $man_hour       =   ManHourMaster::model()->findByPk(1);
        $jobs           =   Job::model()->with('job_category1')->findAll( "job_category1.is_separate = 0" );
        $this->renderPartial('create',array(
                    'model'             =>  $model,
                    'complaint'         =>  $complaint,
                    'work'              =>  $work,
                    'work_complaint'    =>  $work_complaint,
                    'work_order'        =>  $work_order,
                    'man_hour'          =>  $man_hour,
                    'jobs'              =>  $jobs,
                ));
?>
</div>
...

所以当我运行这个代码时,我得到了表单,当我提交它时,我基本上得到了2个相同视图的副本。

1 个答案:

答案 0 :(得分:0)

我尝试了这个,它有效

<?php $form=$this->beginWidget('CActiveForm', array(
        'id'=>'complaint-job-form',
        'enableAjaxValidation'=>true,
        'clientOptions'=>array(
            'validateOnSubmit'=>true,
            'afterValidate'=>'js:function(form,data,hasError){
                        if(!hasError){
                                $.ajax({
                                        "type":"POST",
                                        "url":"'.CHtml::normalizeUrl(array('complaintJob/create','complaint'=>$complaint,'work'=>$work)).'",
                                        "data":form.serialize(),
                                        "success":function(data){
                                        toastr.success("Saved successfully.", "Success");
                                        $("#results").html(data);
                                        $("#ComplaintJob_job_id").select2("val", "");
                                        },

                                        });
                                }
                        }'
        ),
)); ?>