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

Submit form data using Ajax in Yii1

我尝试使用 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
                        )); ?>

我的_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 个副本。

我试过这个,效果很好

<?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", "");
                                        },

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