我一直在寻找解决方案,并尝试了它们,但我找到的答案中没有一个解决了我的问题。这就是它。
我正在一个简单的网络应用程序与输入详细信息和文件上传(.docx文件)。我使用FormData处理表单数据,但服务器端操作只适用于text, password, date等表单输入。
这是我的JS代码;注释行就是我尝试过但不起作用的那行。
var myForm = $("#add-thesis-form");
var formData = new FormData(myForm);
//var formData = myForm.serialize();
$.ajax({
type: "POST" ,
url: "thesis-scripts/add_thesis.php",
data: formData
// processData: false,
// contentType: false
}).done(function(data){
$('#addThesisModal').modal('hide');
alert(data);
//alert("Successfully saved a record to the database. ");
showThesis();
});我的PHP代码(使用Meekro DB)
$title = $_POST["title"];
$description = $_POST["description"];
$categoryId = $_POST["category_id"];
$collegeId = $_POST["college_id"];
$departmentId = $_POST["department_id"];
$uploadedBy = $_SESSION["user"];
$uploadedAt = DB::queryFirstField("SELECT NOW();");
$year = $_POST["thesis_year"];
$keywords = $_POST["keywords"];
$authors = $_POST["authors"];
//FILE UPLOAD
$targ_dir = "../uploads/";
$targ_file = $targ_dir . basename($_FILES["thesis"]["name"]);
$flagOk = 1;
$tempFolder = $_FILES["thesis"]["tmp_name"];
move_uploaded_file($tempFolder, $targ_file);
//!FILE UPLOAD
$result = DB::insert('theses', array(
"title" => $title,
"description" => $description,
"categoryId" => $categoryId,
"collegeId" => $collegeId,
"departmentId" => $departmentId,
"uploadedBy" => $uploadedBy,
"uploadedAt" => $uploadedAt,
"pubyear" => $year,
"filepath" => $targ_file,
"views" => 0,
"rating" => 0,
"keywords" => $keywords,
"authors" => $authors
));我试过那个print_r($_FILES),但这个不行,请帮帮我。谢谢。设置contentType:false会导致服务器对所有$_POST变量抛出错误Undefined index。
发布于 2016-01-14 15:43:42
也许您的表单应该包含enctype="multipart/form-data"
也许这能行得通:
$.ajax({
type: "POST" ,
url: "thesis-scripts/add_thesis.php",
data: "multipart/form-data"
...https://stackoverflow.com/questions/34783755
复制相似问题