I'm trying to upload two files from different input file box without refreshing the page using JQuery, AJAX and PHP. The problem is the files are not uploading, probably because of the way I form.append this two files or the way I define the "data" in AJAX.
Here's my HTML form
<form>
<div class="col col-sm-12 mb-2">
<label class="form-label">Cover Image</label>
<input type="file" class="form-control" name="coverImage" id="coverImage">
</div>
<div class="col col-sm-12">
<label class="form-label">File to Embed</label>
<input type="file" class="form-control" name="fileToEmbed" id="fileToEmbed">
</div>
<button type="button" class="btn btn-primary" name="steganize" id="steganize">STEGANIZE</button>
</form>
Here's the code that I tried doing.
$('#steganize').click(function(){
var file = $('#coverImage').prop("files")[0];
var form = new FormData();
form.append("coverImage", file);
var file2 = $('#fileToEmbed').prop("files")[0];
var form2 = new FormData();
form2.append("fileToEmbed", file2);
$.ajax({
url: "steganize.php",
type: "POST",
data: {form, form2},
contentType: false,
processData:false,
success: function(result){
console.log(result);
}
});
});
Also tried this
$('#steganize').click(function(){
var file = $('#coverImage').prop("files")[0];
var file = $('#fileToEmbed').prop("files")[1];
var form = new FormData();
form.append("coverImage", file);
$.ajax({
url: "steganize.php",
type: "POST",
data: form,
contentType: false,
processData:false,
success: function(result){
console.log(result);
}
});
});
Unfortunately, neither of this two is working. So do you know the proper way of writing it? I'm stuck for hours doing this. Thanks in advance if you could help me.