2014-01-20 3 views
3

저는 Yii와 Ajax를 처음 사용합니다.보기에서 모델의 입력을 읽고 Ajax를 사용하여 저장하려고합니다.Ajax 오류가 발생했습니다.

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

나는 형태

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

및 SAVEFILE() 안에 다음 코드를 사용하고 컨트롤러의 코드는

$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 is blank) 누군가 제게이 문제를 해결하도록 도와주세요. 미리 감사는

+0

양식 코드를 알려주시겠습니까? –

+0

@Ali '$ profile-> attributes = $ _POST ['UserProfile '];' 임 프린팅에서 print_r은 ($가 _ POST)는이 표시 상태에서이 코드 –

+0

를 작동하지 않는 배열 ( [사용자 프로필] => USERPROFILE % 5Babout_me % 5D = tyjtyjjy 및 사용자 프로필 % 5Bcity % 5D = Delhiiii 및 사용자 프로필 % 5Bphone % 5D = 12,111 및 사용자 프로필 % 5Bprofile_picture % 5D = ) like this –

답변

1

코드
$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'];
을 사용했다.

+1

코드 포맷터를 사용하십시오 – crafter

관련 문제