i have a form like this one:
<form>
<div class="characteristicData" id="1">
<textarea></textarea>
<input type="file" name="filelist-1" />
</div>
<div class="characteristicData" id="2">
<textarea></textarea>
<input type="file" name="filelist-2" />
</div>
<input type="button" class="send" value="Send" />
</form>
This form contains three divs to distinguish the data that is needed to send it correctly to the server and set in a DB the correct values for the belonging id.
The data that i wanted to get on the server for $_POST and $_FILES was something like this:
$_POST
Array
(
[characteristic] => Array
(
[element_0] => Array
(
[id_characteristic] => 1
[items] => 0001,0002,0003
)
[element_1] => Array
(
[id_characteristic] => ="2"
[items] => 0001,0002,0003
)
)
)
$_FILES
Array
(
[characteristic] => Array
(
[element_0] => Array
(
[name] => items.csv
[type] => application/vnd.ms-excel
[tmp_name] => tmp/php44DF.tmp
[error] => 0
[size] => 8
)
[element_1] => Array
(
[name] => items.csv
[type] => application/vnd.ms-excel
[tmp_name] => tmp/php44DF.tmp
[error] => 0
[size] => 8
)
)
)
So, my option was trying to create an object containing keys and each key containing the FormData object. At the end, my jQuery is this:
$(function() {
$(".send").click(function() {
var formdata = new FormData(),
$this,
fileDOM = $("input[type='file']")[0];
$(".characteristicData").each(function(i, v) {
$this = $(this);
fileDOM = $this.find("input[type='file']")[0];
formdata.append("characteristic[element_"+i+"][id_characteristic]", $this.attr("id"));
formdata.append("characteristic[element_"+i+"][items]", $this.find("textarea").val());
if ( $this.find("input[type='file']").val() ) {
formdata.append("characteristic[element_"+i+"][file]", fileDOM.files[0], fileDOM.files[0].name);
}
});
$.ajax({
url: "upload.php",
type: "POST",
data: formdata,
processData: false,
contentType: false
});
});
});
That code works fine and when i print the $_POST and $_FILES variables the result is what i expected, but not for the $_FILES. What i get from files is:
Array
(
[characteristic] => Array
(
[name] => Array
(
[element_0] => Array
(
[file] => items.csv
)
[element_1] => Array
(
[file] => items.csv
)
)
[type] => Array
(
[element_0] => Array
(
[file] => application/vnd.ms-excel
)
[element_1] => Array
(
[file] => application/vnd.ms-excel
)
)
[tmp_name] => Array
(
[element_0] => Array
(
[file] => E:\wamp\tmp\php44DF.tmp
)
[element_1] => Array
(
[file] => E:\wamp\tmp\php44E0.tmp
)
)
[error] => Array
(
[element_0] => Array
(
[file] => 0
)
[element_1] => Array
(
[file] => 0
)
)
[size] => Array
(
[element_0] => Array
(
[file] => 8
)
[element_1] => Array
(
[file] => 8
)
)
)
)
Is there any way to get the result that i expected to get?