I am using jQuery, and I would like to upload files with Ajax. I have made some searches and found it is not possible.
However, there is one jQuery plugin, jQuery Form Plugin, which allows us to upload files through Ajax.
It works very well, but I have a special problem. Here is my code:
$('#question-form').submit(function() {
var serialAnswers = '';
// Create a query string given some fields
// Format of the query string : answers[0][fr_fr][0]=a1fr&answers[0][fr_fr][1]=2&answers[0][en_uk][0]=a1en&answers[0][en_uk][1]=6&...
$('#question-answers > div').each(function(idx, elt) {
$('div[lang]', $(elt)).each(function(idxLang, eltLang) {
var lang = $(this).attr('lang');
serialAnswers += 'answers[' + idx + '][' + lang + '][0]=' + $("[answerpart=display]", $(eltLang)).val();
serialAnswers += '&answers[' + idx + '][' + lang + '][1]=' + $("[answerpart=value]", $(eltLang)).val() + '&';
});
});
$(this).ajaxSubmit({
datatype: "html",
type: "POST",
data: serialAnswers,
url: $(this).attr("action"),
success: function(retour) {
$('#res-ajax').html(retour);
}
});
return false;
});
As you can see, I have to replace the $.ajax call by a $(this).ajaxSubmit() call, with the same options. Moreover, I have to create a query string (serialAnswers in the code) according to some fields in order to transmit it to the PHP code.
Here is what I used to do when I had no file to upload. I just serialized the form fields and added my query string named serialAnswers:
$.ajax({
datatype: "html",
type: "POST",
data: $(this).serialize() + '&' + serialAnswers,
url: $(this).attr("action")
success: function(retour) {
$("#res-ajax").html(retour);
}
});
But my problem is that the form plugin transmits my additional data (the query string) that way (in the PHP file):
Array
(
[question_heading_fr_fr] => something
[question_heading_en_uk] => nothing
[question_type] => 5
[0] => a
[1] => n
[2] => s
[3] => w
[4] => e
[5] => r
[6] => s
[7] => [
[8] => 0
[9] => ]
[10] => [
[11] => f
[12] => r
[13] => _
[14] => f
[15] => r
[16] => ]
....
)
According to the documentation, I have to pass a JSON object to the data option, like this:
data: { key1: 'value1', key2: 'value2' }
But I don’t know how to convert my query string to a JSON object and if it would be interpreted as an array on the PHP side.
Is there a solution?
EDIT: Even if I use an iframe, I don’t know how to add a query string with information which does not come from the form (my serialAnswer from the code above).
EDIT: I found a new solution, and it works 🙂
I have modified the jQuery Form Plugin. Plugin page: http://jquery.malsup.com/form/#getting-started. Script: https://github.com/malsup/form/raw/master/jquery.form.js
In order to use this syntax:
instead of:
I have modified the plugin code.
Find this:
And delete all the
if (options.data)and its content. A few lines after these ones, find this:And add this right after it:
In the function fileUpload(), find this:
And replace it by:
You now can add a query string thanks to these modifications and uploading an image at the same time.