I am currently changing my system in order to have a loading progress bar when I now submit my form.
In my old system, I had this form and this script to check if the file exists and is in the right format:
Index.php
<form method="POST" enctype="multipart/form-data" id="myForm" action="upload.php">
<input type="file" name="imageSent" id="imgFile" class="inputImg" />
<label for="imgFile" class="labelForImgF">
<span>Select Img</span>
</label>
<button type="button" class="btnSubmit" onclick='verifyImg();'>
<span>Send</span>
</button>
</form>
<script>
function verifyImg() {
document.getElementById("warning").innerHTML = "";
var fileName = document.getElementById("imgFile");
if(fileName.files.item(0) == null) {
document.getElementById("warning").innerHTML = "You must select an img";
} else {
if(!isValidFileType(fileName.files.item(0).name,'image')) {
document.getElementById("warning").innerHTML = "Bad format";
} else {
document.getElementById('myForm').submit();
document.getElementById("warning").innerHTML = "Sucess";
}
}
}
var extensionLists = {};
extensionLists.image = ['jpg', 'nef', 'bmp', 'png', 'jpeg', 'svg', 'webp', '3fr', 'arw', 'crw', 'cr2', 'cr3', 'dng', 'kdc', 'mrw', 'nrw', 'orf', 'ptx', 'pef', 'raf', 'R3D', 'rw2', 'srw', 'x3f'];
function isValidFileType(fName, fType) {
return extensionLists[fType].indexOf(fName.toLowerCase().split('.').pop()) > -1;
}
</script>
And here is my new system, It works well with ajax but I can't check if the format is correct because as long as I put the onclick:verifyImg(); in my button the form submits without passing by the Ajax system.
Here is my new code:
<form method="POST" enctype="multipart/form-data" id="myForm" action="upload.php">
<input type="file" name="imageSent" id="imgFile" class="inputImg" />
<label for="imgFile" class="labelForImgF">
<span>Select Img</span>
</label>
<button class="btnSubmit">
<span>Send</span>
</button>
</form>
<div id="bararea">
<div id="bar"></div>
</div>
<div id="percent"></div>
<div id="status"></div>
<script>
$(function() {
$(document).ready(function(){
var bar = $('#bar')
var percent = $('#percent');
var status = $('#status');
$('form').ajaxForm({
beforeSend: function() {
status.empty();
var percentVal = '0%';
bar.width(percentVal);
percent.html(percentVal);
},
uploadProgress: function(event, position, total, percentComplete) {
var percentVal = percentComplete + '%';
percent.html(percentVal);
bar.width(percentVal);
},
complete: function(xhr) {
status.html(xhr.responseText);
}
});
});
});
</script>
These two systems work well separately, but I can't mix them, in order to validate my form with javascript and submit it with Ajax.
I think I'm not understanding well how Ajax works, can you help me?
I am a beginner please be indulgent.
Solution: I tried Chris G answer and changed the function beforeSend by beforeSubmit and now It works perfectly.
Code:
<form method="POST" enctype="multipart/form-data" id="myForm" action="upload.php">
<input type="file" name="imageSent" id="imgFile" class="inputImg" />
<label for="imgFile" class="labelForImgF">
<span>Select Img</span>
</label>
<button class="btnSubmit">
<span>Send</span>
</button>
</form>
<div id="bararea">
<div id="bar"></div>
</div>
<div id="percent"></div>
<div id="status"></div>
<script>
function verifyImg() {
document.getElementById("warning").innerHTML = "";
var fileName = document.getElementById("imgFile");
if(fileName.files.item(0) == null) {
document.getElementById("warning").innerHTML = "You must select an img";
return false;
} else {
if(!isValidFileType(fileName.files.item(0).name,'image')) {
document.getElementById("warning").innerHTML = "Bad format";
return false;
} else {
return true;
document.getElementById("warning").innerHTML = "Sucess";
}
}
}
var extensionLists = {};
extensionLists.image = ['jpg', 'nef', 'bmp', 'png', 'jpeg', 'svg', 'webp', '3fr', 'arw', 'crw', 'cr2', 'cr3', 'dng', 'kdc', 'mrw', 'nrw', 'orf', 'ptx', 'pef', 'raf', 'R3D', 'rw2', 'srw', 'x3f'];
function isValidFileType(fName, fType) {
return extensionLists[fType].indexOf(fName.toLowerCase().split('.').pop()) > -1;
}
</script>
<script>
$(document).ready(function(){
var bar = $('#bar')
var percent = $('#percent');
var status = $('#status');
$('form').ajaxForm({
beforeSubmit: function() {
if (!verifyImg()) return false ;
status.empty();
var percentVal = '0%';
bar.width(percentVal);
percent.html(percentVal);
},
uploadProgress: function(event, position, total, percentComplete) {
var percentVal = percentComplete + '%';
percent.html(percentVal);
bar.width(percentVal);
},
complete: function(xhr) {
status.html(xhr.responseText);
}
});
});
</script>

verifyImgreturntrueorfalse, based on whether the file checks out. Then do this in yourbeforeSendfunction:if (!verifyImg()) return false;This should stop the form submission dead in its tracks. Then change your code so clicking the submit button submits the form, soajaxFormtakes over.$(function() { $(document).ready(function(){only$(function() {is needed