Javascript Ajax提交中的错误
我不熟悉Yii和ajax,我想从视图中读取模型的一些输入,并使用ajax保存它。我在表单中使用了以下代码Javascript Ajax提交中的错误,javascript,php,ajax,yii,Javascript,Php,Ajax,Yii,我不熟悉Yii和ajax,我想从视图中读取模型的一些输入,并使用ajax保存它。我在表单中使用了以下代码 <input type="button" name="save" value="save" onclick="saveFile()" id="profile-update" class="btn button-main" live="false"> 但是这里只有profile_图片保存到数据库中,所有其他字段都没有更改。个人资料图片没有复制到文件夹($images为空)请有人帮
<input type="button" name="save" value="save" onclick="saveFile()" id="profile-update" class="btn button-main" live="false">
但是这里只有profile_图片保存到数据库中,所有其他字段都没有更改。个人资料图片没有复制到文件夹($images为空)请有人帮我解决这个问题。提前感谢代码
$profile->attributes=$\u 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\u picture=$\u FILES['file']['name'];
$profile->about_me=$\u POST['UserProfile']['about_me'];
$profile->city=$\u POST['UserProfile']['city'];
$profile->phone=$\u POST['UserProfile']['phone']代码>
我知道这不是正确的方法,但可能对赶时间的人有帮助。您能给我们看一下您的表单代码吗?@Ali'$profile->attributes=$\u POST['UserProfile'];'此代码在im打印打印时不起作用($\u POST),它显示如下数组([UserProfile]=>UserProfile%5About\u me%5D=tyjtyjjy&UserProfile%5Bcity%5D=Delhiiii&UserProfile%5Bphone%5D=12111&UserProfile%5Bprofile\u图片%5D=)
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();
}
}