成功消息不使用ajaxSubmitButton

时间:2014-01-16 12:32:28

标签: php ajax yii

我是yii的新手。我尝试下面的代码,但成功消息不显示bt更改发生在DB中。

我的控制器

public function actionprofileupdate()
    {

        $profile = UserProfile::model()->findByAttributes(array('user_id' => Yii::app()->user->id));
        if (!$profile)
        {
            $profile = new UserProfile;
            $profile->create_time=time();
            $profile->update_time=time();
        }
        if (isset($_POST['UserProfile'])) 
        {

            $profile->attributes = $_POST['UserProfile'];
            $profile->about_me = $_POST['UserProfile']['about_me'];

                $profile->user_id=Yii::app()->user->id;
                $profile->update_time=time();
                $valid = $profile->validate();
                $error = CActiveForm::validate(array($profile));
                if ($error == '[]') 
                {
                    $profile->save(false);
                    echo CJSON::encode(array('status' => 'success'));
                    Yii::app()->end();

                }
                else 
                {
                    $error = CActiveForm::validate(array($profile));
                    if ($error != '[]')
                        echo $error;
                    Yii::app()->end();
                    exit();
                }
            }

    }

并且查看

<div class="tab-pane fade" id="Profilesettings">
                 <div class="tab-heading">Profile settings</div>
                   <div class="errorMessage" id="formResult"></div>
                    <div class="m-t-ss">
<div id="AjaxLoader" style="display: none"></div>
                        <?php 
                        $form = $this->beginWidget('CActiveForm', array(
                            'id' => 'profile-update-form',
                            'enableAjaxValidation' => true,
                            'enableClientValidation' => true,
                            'action' => array('user/profileupdate'),
                            'htmlOptions'=>  array('class' =>'form-horizontal')
                        ));
                        ?>
                        <?php

                        // $model = new User;
                       // $profile=new UserProfile;
                        $model = User::model()->findByPk(Yii::app()->user->id);
                         $profile=UserProfile::model()->findByAttributes(array('user_id'=>$model->id));
                         if(!$profile)
                             $profile=new UserProfile;
                        ?>

                            <div class="form-group">
                                <label for="inputEmail3" class="col-sm-4 control-label text10">About me</label>
                                <div class="col-sm-8">
                                    <?php echo $form->textArea($profile, 'about_me', array('class' => 'form-control form02')); ?>
                                </div>
                                <div class="req"> <?php echo $form->error($profile, 'about_me'); ?> </div>
                            </div>
                            <div class="form-group">
                                <label for="inputEmail3" class="col-sm-4 control-label text10">City</label>
                                <div class="col-sm-8">
                                    <?php echo $form->textField($profile, 'city', array('class' => 'form-control form02', 'id' => 'inputCity')); ?>
                                </div>
                                <div class="req"> <?php echo $form->error($profile, 'city'); ?> </div>
                            </div>
                            <div class="form-group">
                                <label for="inputEmail3" class="col-sm-4 control-label text10">Phone</label>
                                <div class="col-sm-8">
                                     <?php echo $form->textField($profile, 'phone', array('class' => 'form-control form02', 'id' => 'inputPhone')); ?>
                                </div>
                                <div class="req"> <?php echo $form->error($profile, 'phone'); ?> </div>
                            </div>
                            <div class="form-group">
                                <label for="inputEmail3" class="col-sm-4 control-label text10">Profile Picture</label>
                                <div class="col-sm-8">
                                    <?php echo $form->fileField($profile,'profile_picture'); ?>
                                    <?php 
                                    /*$this->widget('CMultiFileUpload', array(
                                    'name' => 'images',
                                    'accept' => 'jpeg|jpg|gif|png',  // useful for verifying files
                                    'duplicate' => 'Duplicate file!', // useful, i think
                                    'denied' => 'Invalid file type', // useful, i think
                                    ));*/
                                    ?>

                                </div>
                                <div class="req"> <?php echo $form->error($profile, 'profile_picture'); ?> </div>
                            </div>
                        <div class="form-group">
                            <label for="inputEmail3" class="col-sm-4 control-label text10"></label>
                            <div class="col-sm-8">

                            <span>
    <?php
    echo CHtml::ajaxSubmitButton('Save', CHtml::normalizeUrl(array('user/profileupdate?rand=' . time())), array(
             'dataType'=>'json',
             'type'=>'post',
             'success'=>'function(data) {
                 $("#AjaxLoader").hide();  
                if(data.status=="success"){
                 $("#formResult").html("profile settings changed successfully.");
                 $("#profile-update-form")[0].reset();
                }
                 else{
                $.each(data, function(key, val) {
                $("#profile-update-form #"+key+"_em_").text(val);                                                    
                $("#profile-update-form #"+key+"_em_").show();
                });
                }       
            }',                    
             'beforeSend'=>'function(){                        
                   $("#AjaxLoader").show();
              }'
             ), array(
        'id' => 'profile-update', 'live' => false, 'class' => 'btn btn-s-md btn-info')
    );
    ?>
</span>
                            </div>
                        </div>
                            <?php $this->endWidget(); ?>

                    </div>
                </div>
请某些人帮帮我

1 个答案:

答案 0 :(得分:0)

这样做 -

'success'=>'js: function(data) {

为beforeSend重复相同操作,如果它仍然无效 - 请检查控制台并使用错误详细信息更新您的问题。