In php i have echo json_encode($show_thumbnails);
On each function (each file upload) with jquery want to create array with current data from php and use the array for the next file upload.
I mean:
1) user clicks Upload
2) send data to php and receive back echo json_encode($show_thumbnails);
3) the received echo json_encode($show_thumbnails); append to jquery array
4) use the array (content of array) at next click Upload
All works except point 4).
Here is my code
$('#upload_file').change(function(e) {
e.preventDefault();
var formData = new FormData($(this).parents('form')[0]);
Just below code does not work. typeof prev_upld_thumbn always is undefined. Below i create array for typeof prev_upld_thumbn, but the array does not exist on the next $('#upload_file').change(function(e) {
if( typeof prev_upld_thumbn !== 'undefined' ) {
formData.append(prev_upld_thumbn);
alert( formData );
}
Here continue code
$.ajax({
url: '___ajax_upload_files.php',
type: 'POST',
xhr: function() {
var myXhr = $.ajaxSettings.xhr();
return myXhr;
},
data: formData,
dataType : 'json',
cache: false,
contentType: false,
processData: false,
success: function (data) {
Here i create var prev_upld_thumbn. And alert( prev_upld_thumbn... works.
var prev_upld_thumbn = [];
prev_upld_thumbn.push( data['first_thumbnail'] );
alert( prev_upld_thumbn + ' prev_upld_thumbn1' );
},
});
});//$('#upload_file').change(function(e) {
Here no alert.
alert( prev_upld_thumbn + ' prev_upld_thumbn2' );
So i have created array prev_upld_thumbn. And inside $('#upload_file').change(function(e) { the array exists.
How to get to exist the array outside $('#upload_file').change(function(e) { or on next call of the function?
In some answers read something about asynchronously, added async: false, but no success