JQuery ajax check and submit form, always have to submit two times when ajax returns nofound

I have this ajax check to find out if a name is found or not. It works great when detecting data, but when it returns nofound, I have to send it again to call it.

Does anyone have a suggestion or a better solution for this?

$('#formletter').submit(function() {
    var name = $('#name').val();

    if ($.formLoading != false) {
        $.ajax({
        type: 'POST',
        url: "submit_ajax_contents.php",
        data: "namecheck="+name,
        success: function(data) {
            if(data == 'nofound'){
            $.formLoading = false;
            $('#formletter').submit();
            }else{
            alert('found');
            $.formLoading = true;
            }
        }
        });
    return false;
    } else {
    return true;
    }

    });

});

Many thanks:)

+3
source share
1 answer

You might want to make a non-asynchronous ajax call: (I have not tested this code)

$('#formletter').submit(function(event) {
    var name = $('#name').val();
    var found = false;
    $.ajax({
        async: false,
        type: 'POST',
        url: "submit_ajax_contents.php",
        data: "namecheck="+name,
        success: function(data) {
            if(data == 'found')
                found = true;
        }
    });
    if (found)
        event.preventDefault();
});
+1
source

Source: https://habr.com/ru/post/1776964/


All Articles