This is my form -
<form id="imgUpIcn" class="imgUpIcn" enctype="multipart/form-data" method="post" action="myfile.php">
<div id="upload-file-container">
<input type="file" name="uploadedImagefile" id="myImageFile" onchange="photoUpInit(event,this,'loadingImg','cropPhLayer')" />
</div>
</form>
This is my photoUpInit()
function -
function photoUpInit(evt, obj, loaderID, cropLBID) {
var ext = obj.value.split('.').pop().toLowerCase(), vStat = true;
if (window.File && window.FileReader && window.FileList) {
var e = evt || window.event, files = e.target.files, fSize = Math.round((parseInt(files[0].size) / 2048) * 100) / 100;
if (fSize > 2048)
vStat = false;
}
if (ext != 'png' && ext != 'jpg' && ext != 'jpeg' && ext != 'gif')
vStat = false;
if (vStat) {
document.getElementById('uplErr').style.display = "none";
document.getElementById(loaderID).style.display = 'block';
//Submitting form here
obj.form.submit();
} else {
document.getElementById(loaderID).style.display = 'none';
document.getElementById('uplErr').style.display = "block";
}
}
Now when the form action is performed my file is getting uploaded on the server but I want to get callback from the php file instead I am redirected to the php page.
Here is my php file-
<?php
if (isset($_FILES['uploadedImagefile']['tmp_name']))
{
$target_path = "../uploads/";
$target_path = $target_path . "tmp";
if(move_uploaded_file($_FILES['uploadedImagefile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedImagefile']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
}
I already tried using this to get callback but its not working-
$('#imgUpIcn').on('submit', (function(e) {
e.preventDefault();
$.ajax({
type : "POST",
url : $(this).attr('action'),
data : $(this).serialize(),
success : function(data) {
$("#myModal").dialog("close");
},
error : function(data) {
console.log("error");
console.log(data);
}
});
}));