dsnrixf6765 2014-01-20 12:12
浏览 72
已采纳

Ajax提交错误

I am new to Yii and ajax I want to read some inputs for a model from view and save it by using ajax. I used the following code inside form

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

and saveFile() is

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;
}

and the code in controller is

$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();
                }
          }

But here only the profile_picture is saving to the database all other fields are not changing. and the profile picture is not copying into the folder($images is blank) Please somebody help me to solve this problem. Thanks in advance

  • 写回答

1条回答 默认 最新

  • doupinge9055 2014-01-21 04:57
    关注

    The code
    $profile->attributes = $_POST['UserProfile'];
    doesnt worked so i send it seperately by
    data.append('UserProfile[about_me]', $('#UserProfile_about_me').val()); data.append('UserProfile[city]', $('#inputCity').val()); data.append('UserProfile[phone]', $('#inputPhone').val());
    and in controller i used
    $profile->profile_picture=$_FILES['file']['name']; $profile->about_me = $_POST['UserProfile']['about_me']; $profile->city = $_POST['UserProfile']['city']; $profile->phone = $_POST['UserProfile']['phone'];
    I know this is not the correct way but may be helpful for someone who is hurry.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看