0

I have a function containing an ajaxSubmit inside. I want it to return a true/false based on the result but it's not working, here the code:

function savePjt(completo){
    completo = completo || 'false';
    $('#pj_form_fields').ajaxSubmit({
        beforeSubmit: function() {
            $('#pjt_submit').val('Loading...');
            $('#pjt_submit').attr('disabled', 'disabled');
        },
        success: function(res) {
            if(res == 'done'){
                if(completo == 'true'){
                    $('#modal_pjt_completo').modal('show');
                    $('#btn_conferma_e_invia').click(function(){
                        $.post(base_url()+'project/completa', {id:$('#id_acquisto').val(), pjt_table:table}, function(resd){
                            return true;
                        });
                    });
                }else{
                    return true;
                }

            }else{
                $('#pjt_submit').removeAttr('disabled');
                alertify.alert(res);
                return false;
            }
        }
    });
}

When I trye to call the function this way:

console.log(savePjt('false'));

The log displays undefined instead of true/false; I even tried returning a number or string but doesn't work...

Any reason I can't see?

Mr.Web
  • 6,992
  • 8
  • 51
  • 86

1 Answers1

1

As ajax is asynchronous ,savePjt is actually returning immediately. So what you are expecting is actually not happening.

MD. Sahib Bin Mahboob
  • 20,246
  • 2
  • 23
  • 45