【发布时间】:2015-07-21 20:43:46
【问题描述】:
我正在使用 ajax 将图像从表单传递到 php 页面。问题是没有获取数据并给出空白输出。以下是我的代码:
test.php
<!-- Help taken from http://stackoverflow.com/questions/24446281/passing-image-using-ajax-to-php -->
<form name="saveImg" enctype="multipart/form-data" id="saveImg">
<input type="file" name="imgVid" id="imgVid">
<button name="submit" id="submit">Upload</button>
<img src="skin/images/process.gif" id="procimg" height="32" width="auto" style="display:none;" />
</form>
<div id="msg"></div>
<!--jQuery-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
document.getElementById("submit").addEventListener("click", function(event){
event.preventDefault();
saveImgfunc();
});
function saveImgfunc(){
var form = new FormData(document.getElementById('saveImg'));
var file = document.getElementById('imgVid').files[0];
if (file) {
form.append('imgVid', file);
}
$.ajax({
type : 'POST',
url : 'core/img.php',
data : form,
cache : false,
contentType : false,
processData : false
}).success(function(data){
console.log(data);
});
}
img.php
<?php
if(isset($_POST['submit'])){
echo "Data can be fetched here";
}
?>
请帮我解决这个问题。 提前致谢。
【问题讨论】:
-
试试 print_r($_FILES)
-
您的网络访问和与此相关的错误日志中显示了什么?
-
如果您使用的是 ie 因为 FormData 支持 ie10+ 而无法正常工作
-
@tiGer。 print_r($_FILES) 正在工作。但我需要将其签入 $_POST。
-
我正在使用 chrome @madalinivascu
标签: php jquery ajax image upload