繁体   English   中英

Ajax提交错误

[英]Error in Ajax submit

我是Yii和ajax的新手我想从视图中读取模型的一些输入并使用ajax保存它。 我在表单中使用了以下代码

<input type="button" name="save" value="save" onclick="saveFile()" id="profile-update" class="btn button-main" live="false">

和saveFile()是

function saveFile()
{
    var data;
    data = new FormData();
    data.append('file', $('#UserProfile_profile_picture')[0].files[0]);
    data.append('UserProfile', $('#profile-update-form').serialize());
    $.ajax({
                url: '<?php echo CHtml::normalizeUrl(array('user/profileupdate?rand=' . time())); ?>',
                type:"POST",
                data: data,
                processData: false,
                contentType: false, 

                success: function (data) {
                    $("#AjaxLoader1").hide();  
                if(data.status=="success"){
                 $("#formResult1").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(){                        
                   $("#AjaxLoader1").show();
              }
                }
            )
            return false;
}

并且控制器中的代码是

$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->profile_picture=$_FILES['file']['name'];
                   $images = CUploadedFile::getInstance($profile,'profile_picture');
                //  print_r($_POST);
                 //  print_r($profile->phone);
                //  print_r($images);
                  // exit();
                   if (isset($images))
                  {
                 if(!is_dir(Yii::getPathOfAlias('webroot').'/images/profilepic/'. 'quy'))
                  {
                  mkdir(Yii::getPathOfAlias('webroot').'/images/profilepic/'. $profile->profile_picture);
                  // the default implementation makes it under 777 permission, which you could possibly change recursively before deployment, but here�s less of a headache in case you don�t
                  } 
                    foreach ($images as $image => $pic)
                     {
                         echo $pic->name;if ($pic->saveAs(Yii::getPathOfAlias('webroot').'/images/profilepic/'.$pic->name))
                        {
                           $profile->profile_picture = $pic->name;
                       }
                  }
                  }
                $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();
                }
          }

但是这里只有profile_picture保存到数据库,所有其他字段都没有变化。 和配置文件图片没有复制到文件夹($ images是空白)请有人帮我解决这个问题。 提前致谢

编码
$profile->attributes = $_POST['UserProfile'];
没有工作,所以我分开发送
data.append('UserProfile[about_me]', $('#UserProfile_about_me').val()); data.append('UserProfile[city]', $('#inputCity').val()); data.append('UserProfile[phone]', $('#inputPhone').val());
在我使用的控制器中
$profile->profile_picture=$_FILES['file']['name']; $profile->about_me = $_POST['UserProfile']['about_me']; $profile->city = $_POST['UserProfile']['city']; $profile->phone = $_POST['UserProfile']['phone'];
我知道这不是正确的方法,但对于急于求成的人可能会有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM