I have a portion of a form that is submitted by ajax to a PHP script I can't submit the all form, I can't grab the all formData:
var formData = new FormData($('#myform')[0]);
What I do is read the value of text input fields, and post them via ajax ( with jQuery ) and it works ok with test field.
After this sample code I catch the ajax result :
var field1 = $('#field1').val();
var field2 = $('#field2').val();
var postData = {
'field1': field1,
'field2': field2
};
var formInvio = $.ajax({
url: 'ajax-post.php',
type: 'POST',
data: postData,
dataType: 'json',
async: false,
cache: false,
encode: true
});
The problem is that I don't know how to get an image input file and add it to postData
In the form I have a file field:
<input type="file" name="myimg" id="myimg">
but reading val()
doesn't work:
var myimg = $('#myimg').val();