I am trying to upload an image using PHP, AJAX and jQuery. The image is uploaded perfectly without the ajax i.e using only submit_post.php but when I use AJAX it does not succeed and comes up with an "error" alert. I am surely missing something in the AJAX submit script.
Here is the script
submit.js
function submitForm(){
$.ajax({
type : 'POST',
url : 'submit_post.php',
data : $('#submit_post').serialize(),
dataType : 'json',
mimeType: "multipart/form-data",
success : function(data){
console.log(data);
$('#btn-signup').html('<img src="ajax-loader.gif" /> signing up...').attr('disabled', 'disabled');
setTimeout(function(){
if ( data.status==='success' ) {
$('#errorDiv').slideDown(200, function(){
$('#errorDiv').html('<div class="alert alert-info">'+data.message+'</div>');
$('#errorDiv').delay(3000).slideUp(100);
$("#submit_post")[0].reset();
$('#btn-signup').html('<span class="glyphicon glyphicon-log-in"></span> Sign Me Up');
$('#btn-signup').removeAttr('disabled');
});
} else {
$('#errorDiv').slideDown(200, function(){
$('#errorDiv').html('<div class="alert alert-danger">'+data.message+'</div>');
$('#errorDiv').delay(3000).slideUp(100);
$('#btn-signup').html('<span class="glyphicon glyphicon-log-in"></span> Sign Me Up');
$('#btn-signup').removeAttr('disabled');
});
}
},3000);
},
error: function(){alert('Error!')}
});
return false;
}
});
It doesn't have to do with the folder permissions because the image is uploaded fine without the AJAX.