AJax表单提交,无法加载文件

时间:2018-04-13 07:28:38

标签: php ajax forms

进行ajax表单提交,调用已完成,但php脚本未执行。

<form method="post" id="uform" enctype="multipart/form-data">
Name<input type="text" name="name" required>
URL<input type="file" name="presentation" required>
<button type="submit" value="submit"></button>
</form>

脚本: -

<script type="text/javascript">
 $("form#uform").submit(function(e) {
e.preventDefault();
var fd= document.getElementById('uform');
var formData = new FormData(fd);
var ajax = new XMLHttpRequest();
ajax.upload.addEventListener("progress", progressHandler, false);
ajax.addEventListener("load", completeHandler, false);
ajax.addEventListener("error", errorHandler, false);
ajax.addEventListener("abort", abortHandler, false);
ajax.open("POST", "upload_video_process.php");
ajax.send(formData);
});
</script>

PHP:

if(isset($_POST) && !empty($_FILES['presentation']['name']))
{
//-------Code to be executed
}
else{
echo "error2";
}

我在回复时得到error2意味着没有设置$ _FILES ['presentation'] ['name']。不知道该怎么做。devtools snap of ajax request

1 个答案:

答案 0 :(得分:0)

使用此:

<form method="post" id="uform" enctype="multipart/form-data">

您的表单标记缺失&#39; enctype&#39;如果您的表单包含&#39;文件,则需要该属性。输入类型。