CActiveForm中的ajax请求

时间:2012-04-26 11:26:19

标签: php jquery ajax yii

我在yii框架中尝试基于ajax的表单提交这里是我的视图代码

<div class="form">

<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'users-form',
    'enableAjaxValidation'=>true,
    'clientOptions'=>array('validateOnSubmit'=>true, 'validateOnType'=>false),
)); ?>

    <p class="note">Fields with <span class="required">*</span> are required.</p>

    <?php echo $form->errorSummary($model); ?>

    <div class="row">
        <?php echo $form->labelEx($model,'user_username'); ?>
        <?php echo $form->textField($model,'user_username',array('size'=>45,'maxlength'=>45)); ?>
        <?php echo $form->error($model,'user_username'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'user_password'); ?>
        <?php echo $form->textField($model,'user_password',array('size'=>45,'maxlength'=>45)); ?>
        <?php echo $form->error($model,'user_password'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'user_role'); ?>
        <?php echo $form->dropDownList($model,'user_role', CHtml::listData($dropDownData, 'role_id', 'role_title')); ?>
        <?php echo $form->error($model,'user_role'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'user_company_id'); ?>
        <?php echo $form->textField($model,'user_company_id'); ?>
        <?php echo $form->error($model,'user_company_id'); ?>
    </div>

    <div class="row buttons">
        <?php echo CHtml::ajaxSubmitButton('Create User', CHtml::normalizeUrl(array(''), 
            array(
                'beforeSend'=>'function() {
                                    $(".createUser").attr("disabled","disabled");
                                }',
                'success'=>'function(data) {
                                alert(data);
                                if( data == "sent" ) {
                                    $(".ajaxMessage").text("User has been successfully created");
                                } else {
                                    $(".ajaxMessage").text("Oops! It looks there is some error in your form");
                                }
                                $(".createUser").removeAttr("disabled");
                            }'
            ),
            array('class'=>'createUser')
        )); ?>
    </div>

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

</div><!-- form -->
<div class="ajaxMessage"></div>

我想在变量中获取ajax响应。这是控制器代码

if(isset($_POST['Users']))
{
    if( Yii::app()->request->isAjaxRequest )
        echo 'ajax get';
    else
        echo 'ajax not get';
}

在firebug中我看到响应是正确生成的,但它没有存储在我的变量中。视图代码是在此链接的帮助下创建的:
http://www.yiiframework.com/forum/index.php/topic/8219-solved-ajax-form-submission/

1 个答案:

答案 0 :(得分:2)

javascript data将是您从控制器回复的任何内容。因此,如果您想在jquery的data=="sent"回调中检查success,请从操作中执行echo 'sent';

您已在normalizeUrl函数中包含ajaxOptions,将其更改为:

<div class="row buttons">
    <?php echo CHtml::ajaxSubmitButton('Create User', CHtml::normalizeUrl(array('')), 
        array(
            'beforeSend'=>'function() {
                                $(".createUser").attr("disabled","disabled");
                            }',
            'success'=>'function(data) {
                            alert(data);
                            if( data == "sent" ) {
                                $(".ajaxMessage").text("User has been successfully created");
                            } else {
                                $(".ajaxMessage").text("Oops! It looks there is some error in your form");
                            }
                            $(".createUser").removeAttr("disabled");
                        }'
        ),
        array('class'=>'createUser')
    ); ?>