I am uploading a file to PHP and want to return errors in an alert or better yet in a div element. It seems that when I do not use alert, the function always fails to produce the desired result. Although pressing ctrl-F5 (chrome) to bypass caching sometimes works, but sometimes the following console message is shown:
writers.php:1 Uncaught ReferenceError: phpReturn is not defined
JS call to PHP
$('#writers').on('submit', function () {
if (beforeSubmit()) {
$(this).ajaxSubmit(options);
} else return false;
});
This works just fine.
RelevantPHP error handler
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime_type = finfo_file($finfo, $UploadDirectory . $NewFileName);
finfo_close($finfo);
if ($mime_type != 'application/pdf') {
unlink($UploadDirectory . $NewFileName);
echo "<script type='text/javascript'>phpReturn('File MUST be a PDF!');</script>";
}
JS function called from PHP
function phpReturn(errmsg) {
if (errmsg != undefined) {
alert(errmsg);
$("#errmsg").text(errmsg).css({ 'display': 'block' });
}
window.location.href = "http://writers-tryst.ron-tornambe.com/#/writers";
It is a little tricky to test this behavior. You need to rename a non-PDF file to one with a PDF extension. Here is the test site.