1

我正在 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';
}

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

4

1 回答 1

2

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

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

<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')
    ); ?>
于 2012-04-26T11:59:14.650 回答