Php 使用ajax以表单提交的形式上传图像
我已经用Ajax提交了表单,我无法在此提交中上载图像Php 使用ajax以表单提交的形式上传图像,php,ajax,Php,Ajax,我已经用Ajax提交了表单,我无法在此提交中上载图像 <form id="forms-items" name="forms-items" method="post" enctype="multipart/form-data"> <input id="uploadBtn" name="uploadimg" type="file" class="upload" /> </form 输出:请选择一个文件 JavaScript代码 使用该类最简单: 现在您有了一个
<form id="forms-items" name="forms-items" method="post" enctype="multipart/form-data">
<input id="uploadBtn" name="uploadimg" type="file" class="upload" />
</form
输出:请选择一个文件
JavaScript代码
使用该类最简单:
现在您有了一个FormData对象,可以与XMLHttpRequest一起发送。并使用FormData对象附加字段
<script type="text/javascript">
$(document).ready(function () {
var form = $('#forms-items'); //valid form selector
form.on('submit', function (c) {
c.preventDefault();
var data = new FormData();
$.each($(':input', form ), function(i, fileds){
data.append($(fileds).attr('name'), $(fileds).val());
});
$.each($('input[type=file]',form )[0].files, function (i, file) {
data.append(file.name, file);
});
$.ajax({
url: 'ajax/submitform.php',
data: data,
cache: false,
contentType: false,
processData: false,
type: 'POST',
success: function (c) {
//code here if you want to show any success message
alert(response);
}
});
return false;
});
})
</script>
并强制jQuery不为您添加内容类型头,否则,上载文件边界字符串将丢失。您可以粘贴js代码吗$target\u路径是什么?你已经创建了2个else…你是说if,else if和else?你能粘贴整个PHP代码吗?这篇文章似乎有一部分被删掉了,因为if,if,elseYou缺少一个{。这是一个复制/粘贴错误吗?没有语法错误问题是这段代码找不到我想要上传的文件
$("#forms-items").submit(function()
{
$.ajax({
url: "ajax/submitform.php",
type: "POST",
data: $("#forms-items").serialize(),
success: function(response) {
alert(response);
}
});
});
<script type="text/javascript">
$(document).ready(function () {
var form = $('#forms-items'); //valid form selector
form.on('submit', function (c) {
c.preventDefault();
var data = new FormData();
$.each($(':input', form ), function(i, fileds){
data.append($(fileds).attr('name'), $(fileds).val());
});
$.each($('input[type=file]',form )[0].files, function (i, file) {
data.append(file.name, file);
});
$.ajax({
url: 'ajax/submitform.php',
data: data,
cache: false,
contentType: false,
processData: false,
type: 'POST',
success: function (c) {
//code here if you want to show any success message
alert(response);
}
});
return false;
});
})
</script>