0

I need to validate a user and password before a form is sent. For that I use jQuery and Ajax. But I have problems passing the 'estado'value outside the anonymous function.

This is the code:

function tx_oriconvocatorias_pi1_validarFormPost(){
    var x=document.forms["form_postulante"]["pass_un"];
    if (x.value==null || x.value==""){
        alert("Por favor ingrese su contraseña.");
        return false;
    }
    var $j = jQuery.noConflict();
    var cun = $j('#correo_un').val();
    var pun = $j('#pass_un').val();
    var estado = null;
    $j.ajax({
        type: 'POST',
        data: 'eID=ori_convocatorias_formPost&correo_un='+cun+'&pass_un='+pun,
        success: function(datos){
            if(datos!=1){
                alert ("La contraseña ingresada no es válida"); 
            }
            estado = datos;
            alert(estado);
        },
    });
    alert(estado);
    if (estado!=1){
        return false;
    }
}

I notice that the first alert(estado)(outside) returns null and then the second alert(estado) (inside) returns 'datos', so the last alert(estado) in the code is executed first and always my function returns false.

I don't know how to evaluate 'estado' after execute the Ajax code and not before.

Thanks in advance for the help.

3 Answers 3

1

You can't. You are doing an asynchronus request so your function ends before the result of the ajax call has returned. You can force the call to be synchronus :

$j.ajax({
        type: 'POST',
        data: 'eID=ori_convocatorias_formPost&correo_un='+cun+'&pass_un='+pun,
        async: false,//here you are synchrone
        success: function(datos){

or you can return false, and submit your form manually.

Sign up to request clarification or add additional context in comments.

Comments

1

The reason the first alert(estado) is null and the second alert(estado) is 'datos', is because ajax is asynchronous. This means that even though you are calling $j.ajax, the code is still going through and evaluating the rest of the function.

I think this might be something more of what you're looking for:

function tx_oriconvocatorias_pi1_validarFormPost(){
    var x=document.forms["form_postulante"]["pass_un"];
    if (x.value==null || x.value==""){
        alert("Por favor ingrese su contraseña.");
        return false;
    }
    var $j = jQuery.noConflict();
    var cun = $j('#correo_un').val();
    var pun = $j('#pass_un').val();
    var estado = null;
    $j.ajax({
        type: 'POST',
        data: 'eID=ori_convocatorias_formPost&correo_un='+cun+'&pass_un='+pun,
        success: function(datos){
            if(datos!=1){
                alert ("La contraseña ingresada no es válida"); 
            }
            estado = datos;
            alert(estado);
        },
        error: function(datos) {
            return false;
       }
    });
}

1 Comment

Thanks, the code you wrote is not exactly what I need, but you're right about that I was using asynchronous when I must use synchronous.
1

You have to use synchronous call in order to achieve this. But with synchronous ajax calls the browser will not respond untill the response comes keep this in mind.

1 Comment

Thanks for the comment, I don't have problem with this delay because the function is used to validate a form against a database before it is sent.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.