Before submitting a form, I want to validate the data by calling a PHP page, which returns an empty string if everything is okay and an error message if not. My current code works, but async: false is deprecated. I’m having trouble finding an alternative that works. Can you help me?
Thank you!
$("form").on("submit", function(event) {
let idForm = $(this).attr("id");
if(correspFormCtrl[idForm]) {
let dataFormArray = $(this).serializeArray();
let dataForm = dataFormArray.reduce((acc, item) => {
acc[item.name] = item.value;
return acc;
}, {});
let cible = $(this);
let ok = false;
$.ajax({
url : "php/verifHtmlForm.php",
type: "POST",
async: false,
data: { listeChamps: correspFormCtrl[idForm].champs, valeursForm: dataForm},
success: function(data) {
if(data) {
cible.find(".retourErreurForm").removeClass("invisible")[0].scrollIntoView({ block: 'start' });
cible.find(".infosErreurHtml").html(data);
}
else {
cible.find(".retourErreurForm").addClass("invisible");
ok = true;
}
},
complete : function() {
if(!ok) {
event.preventDefault();
return false;
}
},
error: function(error) {
console.error(error);
}
});
}
});
I try Promise, but don’t know exactly how to use or if it’s appropriate.