【发布时间】:2016-02-19 02:11:40
【问题描述】:
我正在尝试通过 ajax 上传文件以及表单中的一些字段。但是,它不起作用。我收到此错误。
未定义索引:- 文件
这是我的代码。
HTML
<!-- File Button -->
<div class="form-group">
<label class="col-md-4 control-label" for="file">Upload Software / File</label>
<div class="col-md-4">
<input id="file" name="file" class="input-file" type="file">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label" for="price">Price($)</label>
<div class="col-md-4">
<input id="price" name="price" type="text" placeholder="Price" class="form-control input-md" required="">
</div>
</div>
Ajax
$("#add_product").click(function(e){
e.preventDefault();
product_name = $("product_name").val();
//d = $("#add_new_product").serialize();
$.ajax({
type: 'POST',
url: 'ajax.php',
data: $("#add_new_product").serialize(),
success: function(response)
{
//
alert(response);
}
})
});
PHP
if (0 < $_FILES['file']['error'])
{
echo ":!";
}
else
{
echo "ASa";
}
我在这里错过了什么?
【问题讨论】:
-
@M.Doye 这里的问题是,我有更多的数据和文件一起造成了麻烦。
-
序列化函数不会在数据变量中包含文件有另一种机制来实现这一点,请谷歌它。
-
您可以使用@M.Doye 解决方案,您必须在提交表单而不是文件更改事件时触发代码。
-
看看这个,它完全符合您的要求。 stackoverflow.com/questions/10899384/…
标签: javascript php jquery ajax file-upload