How to continue form submission after an AJAX call?

后端 未结 4 421
陌清茗
陌清茗 2021-01-14 11:08

I want to validate user entries on a WordPress post upon hitting the submit button, display an error message is there are problems, and submit the form if everything is OK.

4条回答
  •  忘掉有多难
    2021-01-14 12:02

    The real question is - why are you doing validation server-side? Why can't you load in the validation criteria before - as the post is being written? Then your validation can happen real-time and not on-submit.

    jquery.post is performed asynchronously, which means the JS will continue before it gets the reply. You're stuck with Diodeus's answer - bind the button to validtion which then submits the form (which makes it not degrade well), or change your $.post to ajax and turn off async, which will force it to wait for response before proceeding...possibly locking up JS on your page until it times out.

    $.ajax({
        type: 'POST',
        url: ajaxurl,
        async:false,
        data: data,
        timeout:3000,
        success: function(){
    
        }
    });
    

提交回复
热议问题