it’s easiest to use the FormData()
class:
So now you have a FormData object, ready to be sent along with the XMLHttpRequest. and append fields with FormData Object
<script type="text/javascript">
$(document).ready(function () {
var form = $('#forms-items'); //valid form selector
form.on('submit', function (c) {
c.preventDefault();
var data = new FormData();
$.each($(':input', form ), function(i, fileds){
data.append($(fileds).attr('name'), $(fileds).val());
});
$.each($('input[type=file]',form )[0].files, function (i, file) {
data.append(file.name, file);
});
$.ajax({
url: 'ajax/submitform.php',
data: data,
cache: false,
contentType: false,
processData: false,
type: 'POST',
success: function (c) {
//code here if you want to show any success message
alert(response);
}
});
return false;
});
})
</script>
and forcing jQuery not to add a Content-Type header for you, otherwise, the upload file boundary string will be missing from it.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…