Przerabiam skrypt uploadu obrazków PHP + jQuery (ajax) i w związku z tym mam kilka pytań:
1) mam takie coś:
// Add events
$('input[type=file]').on('change', prepareUpload);
$('form').on('submit', uploadFiles);
i teraz po wybraniu obrazka skrypt wywołuje funkcję prepareUpload
// Grab the files and set them to our variable
function prepareUpload(event)
{
files = event.target.files;
}
i teraz po wybraniu obrazka skrypt i kliknięciu submit, skrypt wywołuje funkcję uploadFiles, która AJAXem wysyła do skryptu PHP informacje z formularza tj. z pola input[type="file"]
function uploadFiles(event)
{
event.stopPropagation(); // Stop stuff happening
event.preventDefault(); // Totally stop stuff happening
files = event.target.files;
// START A LOADING SPINNER HERE
// Create a formdata object and add the files
var data = new FormData();
$.each(files, function(key, value)
{
data.append(key, value);
});
$.ajax({
url: './submit.php?files',
type: 'POST',
data: data,
cache: false,
dataType: 'json',
processData: false, // Don't process the files
contentType: false, // Set content type to false as jQuery will tell the server its a query string request
success: function(data, textStatus, jqXHR)
{
if(typeof data.error === 'undefined')
{
// Success so call function to process the form
submitForm(event, data);
}
else
{
// Handle errors here
console.log('ERRORS: ' + data.error);
}
},
error: function(jqXHR, textStatus, errorThrown)
{
// Handle errors here
console.log('ERRORS: ' + textStatus);
// STOP LOADING SPINNER
}
});
}
i pytanie jest następujące - jak zrobić, żeby po wybraniu obrazka skrypt automatycznie go uploadował? żeby pominąć to on 'submit' a dać to w on 'change'. Podmieniłem nazwy funkcji ale nie działa upload wtedy - nie przesyła pliku. Jak połączyć te funkcje?
f5