I have a problem that I can not solve, it turns out that I want to call a function which after doing an insert in mysql returns the id, that makes it perfect, but my function never manages to receive the result despite the id by console It comes out correctly, what am I doing wrong?
Now if instead of calling the function I hit it below if doing another function walks. As it does not wait for the return of the function and continues long A thousand thanks
example console log gives me
index.php? mod = documents & new: 272 Document id undefined index.php? mod = documents & new: 305 The generated id is 12
var idDocumento=0;
function newfun(){
var termino=false;
idDocumento=SaveDoc(); //<-- nunca regresa el id creado no entiendo porque sale undefined
if (idDocumento==0){
console.log ("No pudo guarda el doc.");
}
else
{
console.log ("Documento id "+idDocumento);
if (termino==true){
location.reload();
}
}
}
function SaveDoc(){
//Grabo el documento
var claveingresado=0;
var sigue=0;
if($(seguimiento).is(':checked')){
sigue=1;
}
var parametros = {
"valorCaja1" : $('#asunto').val(),
"valorCaja2" : $('#nota').val(),
"valorCaja3" : sigue
};
$.ajax({
data: parametros,
url: 'grabo-doc.php',
type: 'post',
beforeSend: function () {
$("#recargado").html("Procesando, espere por favor...");
},
success: function (response) {
$("#recargado").html(response);
idDocumento=response; //id creado
console.log("El id generado es "+idDocumento); //<----- aca lo muestra perfecto
return idDocumento; //<---deberia regresar ese valor
}
});
}