我正在努力将一个图像文件和少量数据发送到PHP文件。所以我使用表格来做到这一点。这是我的JS代码;
var pictureForm = new FormData();
var imgFile = $('#avatarSelect')[0];
pictureForm.append('email', email);
pictureForm.append('firName',editfirName);
pictureForm.append('lstName',editlstName);
pictureForm.append('newPass',editpass);
pictureForm.append('newPhn', editphnNum);
pictureForm.append('pictureFile', imgFile.files[0]);
$.ajax({
type: "POST",
url: "php/accountUpdate.php",
dataType:'json',
data:{pictureForm},
processData: false,
contentType: false,
success : function(response)
{
if(response==1)
{
alert("Success");
}
else
{
alert(response);
}
}
});
和accountUpdate.php;
<?php
if($_POST)
{
$userEmail= $_POST['email'];
$userPass= $_POST['newPass'];
$userPhone = $_POST['newPhn'];
$userFName= $_POST['firName'];
$userLName = $_POST['lstName'];
$servername ="localhost";
$username="root";
$password="";
$dbname="AS2014459";
// Some code
}
else
echo json_encode("Error");
?>
我的问题
但是这总是提醒我“错误”。意味着$ _POST未正确设置。我找不到原因。但所有变量,如email,editfirname ......等都已正确设置并具有值。
当我在本地服务器上运行它时(删除PHP if($ _ POST)条件)控制台日志说未定义索引:电子邮件。任何猜测?
答案 0 :(得分:0)
您还没有关闭if / else语句,因此每次都会回显错误并使用isset($ _ POST)
答案 1 :(得分:0)
尝试使用布尔值( true )更改 1
var pictureForm = new FormData();
var imgFile = $('#avatarSelect')[0];
pictureForm.append('email', email);
pictureForm.append('firName',editfirName);
pictureForm.append('lstName',editlstName);
pictureForm.append('newPass',editpass);
pictureForm.append('newPhn', editphnNum);
pictureForm.append('pictureFile', imgFile.files[0]);
$.ajax({
type: "POST",
url: "php/accountUpdate.php",
dataType:'json',
data:{pictureForm},
processData: false,
contentType: false,
success : function(response)
{
if(response==true)
{
alert("Success");
}
else
{
alert(response);
}
}
});