Good morning.
I'm doing a script in which ajax performed a process of insertion in BD depending on what returns this function ajax I need to reload the same page or clean the form, if everything goes well continue with another function ajax in the which redirects to another page.
My problem is that regardless of the result of the first function it always redirects to the address of the second function and I need that if the condition on the first ajax call is not fulfilled recharge the same page but in this case it continues with the execution.
I am using "promises". Any idea, suggestion to do this or some way that is better?
I leave my code so you can see what I'm trying.
$(document).ready(function () {
$("#btnRegistroExamen").click(function (e) {
e.preventDefault();
var id = $("#Userid").text(),
FechaExamen = $("#Fechas").val(),
FolioP = $("#FolioP").val(),
FechaPago = $("#fechaP").val(),
name = $("#nombre").val(),
lastName = $("#Apellidos").val(),
semestre = $("#Semestre").val(),
carrera = $("#Carrera").val(),
email = $("#email").val(),
nControl = $("#Nctrl").val();
var datos = {
"id": id,
"FechaExamen": FechaExamen,
"FolioP": FolioP,
"FechaPago": FechaPago
}
var completeData = {
"id": id,
"FechaExamen": FechaExamen,
"FolioP": FolioP,
"FechaPago": FechaPago,
"nombre": name,
"apellido": lastName,
"semestre": semestre,
"carrera": carrera,
"email": email,
"nControl": nControl
};
var Registro = $.ajax({
url: "php/RegistrarExamen.php",
data: datos,
method: "POST",
beforeSend: function () {
console.log("Enviando datos");
},
success: function (data) {
console.log("Excito en envio de info...");
if (!data) {
console.log(data);
alert(data);
location.reload();
}
},
error: function () {
console.log("Fallo en envio");
}
});
Registro.promise().done(function () {
//Generacion de QR
$.ajax({
url: "php/qrGenerator.php",
data: completeData,
method: "POST",
beforeSend: console.log("Enviando QR"),
success: function (data) {
if (data) {
location.href = 'RExamenExito.php?id=${id}';
} else {
console.log("Error en generacion de QR");
}
},
error: function () {
console.log("Error en QR");
}
});
});
});
});
Thank you in advance for your help and comments.
Greetings.