开发者

jQuery ajax check and submit form, always have to submit twice when ajax return nofound

开发者 https://www.devze.com 2023-01-27 13:06 出处:网络
I have this ajax check to see if the name is found or nofound. It works fine when data retur开发者_StackOverflow中文版ns found, but when it return nofound, I must submit it again to trigger it.

I have this ajax check to see if the name is found or nofound. It works fine when data retur开发者_StackOverflow中文版ns found, but when it return nofound, I must submit it again to trigger 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 :)


You might want to do a non-async ajax call: (I didn't test 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();
});
0

精彩评论

暂无评论...
验证码 换一张
取 消