Reputation: 129
It's working but on every submit I receive an array even tho a file is not even added to the file input (multifile input)
postData = new FormData(this);
$.ajax({
url: "/url",
type: "POST",
data: postData,
cache: false,
contentType: false,
processData: false,
success: function (data, textStatus, jqXHR) {
if (data === "true") {
window.location.replace("/url");
} else {
$(".errors").html(data);
}
},
error: function (jqXHR, textStatus, errorThrown) {
swal("Der opstod en fejl");
}
});
What i get from $_FILES is the following
Array
(
[files] => Array
(
[name] => Array
(
[0] =>
)
[type] => Array
(
[0] =>
)
[tmp_name] => Array
(
[0] =>
)
[error] => Array
(
[0] => 4
)
[size] => Array
(
[0] => 0
)
)
)
Is there a way where I can avoid this from happening?
Upvotes: 0
Views: 116
Reputation: 1
Try adding required
attribute to input type="file"
element to prevent submission of form
if no files selected by user
$("form").on("submit", function(e) {
e.preventDefault();
// do `$.ajax()` stuff
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<form>
<input type="file" name="files[]" multiple required />
<input type="submit" />
</form>
Upvotes: 1