javascript - Ajax 提交错误

标签 javascript php ajax yii

我是 Yii 和 ajax 的新手,我想从 View 中读取模型的一些输入并使用 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;
}

Controller 中的代码是

$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['U​​serProfile'];
不起作用,所以我通过
data 单独发送.append('UserProfile[about_me]', $('#UserProfile_about_me').val()); data.append('UserProfile[city]', $('#inputCity').val()); data.append('UserProfile[phone]', $('#inputPhone').val());
在 Controller 中我使用了
$profile->profile_picture= $_FILES['文件']['名称']; $profile->about_me = $_POST['U​​serProfile']['about_me']; $profile->city = $_POST['U​​serProfile']['city']; $profile->phone = $_POST['U​​serProfile']['phone'];
我知道这不是正确的方法,但可能对赶时间的人有帮助。

关于javascript - Ajax 提交错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21233779/

相关文章:

javascript - 是否可以将 firebase 的图片上传 "uploadTask.on"监听器变成一个 promise?

javascript - 如何在关闭模式后保留选中的复选框?

javascript - js - appendChild 方法破坏整个页面

javascript - 在 javascript 中使用 Public 关键字时出现意外标记

php - 在 php 中发布复选框值

php - Wordpress REST API 自定义帖子类型 禁止删除访问

javascript - Google Chrome XHR 预览选项卡背景变灰

php - 最高分 - 从数据库中对数字进行排序

javascript - 如果没有连接,XHR 返回什么?

ajax - HTTP Keep Alive 对 AJAX 站点的好处