S
S
Sergey Ivchenko2015-12-20 22:29:10
css
Sergey Ivchenko, 2015-12-20 22:29:10

How to execute form.submit on ajax success?

Tell me, please, how can I execute form.submit inside the success of the Ajax request? If it matters, then the ajax request is made using the BX.ajax method. The browser (Mozilla) when trying to submit the form writes that Firefox blocked one pop-up window from the site.

$('#myform').validate({
    debug: true,
    rules : {
      email : {
        required : true,
        email : true
      }
    },
    messages : {
      email : {
        required : '<div class="input-error">Введите, пожалуйста, ваш e-mail</div>',
        email : '<div class="input-error">Некорректный e-mail</div>'
      }
    },
    submitHandler: function(form) {
      //Получим данные формы
      var data = $(form).serialize();
      console.log(data);
      //Выполним ajax запрос
      BX.ajax({
        url: 'myajax.php',
        method: 'POST',
        data: data,
        dataType: 'json',
        onsuccess: function(response) {
          if (response.status == 'OK') {
            $(form).find('input[name="orderNumber"]').val(response.orderNumber);
            console.log($(form));
            form.submit();
          }
        }
      });
    }
  });


data and form are.
console.log($(form));
works and produces the correct result.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
dimon119, 2015-12-20
@dimon119

$.ajax({
...
success: function(data){
$("#form").submit();
}
});

S
Sergey Ivchenko, 2015-12-21
@Serg89

Thanks to all. Found a solution.
You need to tell ajax that the request is synchronous using a parameter

async: false
. With a successful request for some variable, we set
true
.
When leaving ajax, we check this variable for validity and in this case we execute
form.submit();

F
fabrykant, 2015-12-20
@fabrykant

$("form").trigger( "submit" );

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question