var requestUrl = '/check_capcha.php'; $('body').on('submit', 'form.me', function (e) { var postData = $("form.me").serialize(); console.log(postData); $.ajax({ type: "POST", data: postData, url: requestUrl, success:function(data){ console.log(data); } }); e.preventDefault(); }); 

How do i e.preventDefault(); insert in date check? page code check_capcha.php:

 if (!$APPLICATION->CaptchaCheckCode($_POST["captcha_word"], $_POST["captcha_sid"])) { echo 0; } else{ echo 1; } 
  • The question will not be deleted as last time? Is it worth writing the answer? And in what condition do you want to insert it? What should happen if the response from the server is 1 or 0? - Yaroslav Molchan
  • @YaroslavMolchan just after the publication immediately saw an error. And she did not carry any cognitive meaning for other participants to leave the question and give the answer to it. Just corny there is not inserted the code. I have a condition e.preventDefault(); , but I need to use it only when the answer from the server is 0 - Oleksandr

1 answer 1

I would not do much differently, in any case I would stop the event of submitting the form, but if the form passed validation, it means submitting the form again, but already without binds to submit this form. Here is your example not much modified:

 var requestUrl = '/check_capcha.php'; $('body').on('submit', 'form.me', function (e) { e.preventDefault(); var postData = $("form.me").serialize(); console.log(postData); $.ajax({ type: "POST", data: postData, url: requestUrl, context: this, success:function(data){ console.log(data); if (data == 1) { //Валидация прошла успешно, самбитим форму ещё раз, но уже не обрабатываем событие $(this).unbind('submit').submit(); } else { //Валидацию не прошел, выполняем js дальше } } }); }); 

The downside is that if there are more handlers for this event, they will not work out either.