Alexey Vesnin2018-01-23 17:24:38
Alexey Vesnin, 2018-01-23 17:24:38

Ajax form is processed incorrectly, how to fix the error?

Hello! Need help. There is a JS form handler:

$(document).ready(function () {
    $("form").submit(function () {
        // Получение ID формы
        var formID = $(this).attr('id');
        // Добавление решётки к имени ID
        var formNm = $('#' + formID);
        var message = $(formNm).find(".msgs"); // Ищет класс .msgs в текущей форме  и записываем в переменную
        var formTitle = $(formNm).find(".formTitle"); // Ищет класс .formtitle в текущей форме и записываем в переменную
            type: "POST",
            url: '/assets/modalform/mail.php',
           // url: '/mail.php',
            data: formNm.serialize(),
            success: function (data) {
                // Вывод сообщения об успешной отправке
                    $('input').not(':input[type=submit], :input[type=hidden]').val('');
                    $('textarea').not(':input[type=submit], :input[type=hidden]').val('');
                }, 9000);
                // $('.remodal-close').click();
            error: function (jqXHR, text, error) {
                // Вывод сообщения об ошибке отправки
                // $(formNm).css("display","none");

                }, 3000);
        return false;
    //для стилей формы
    var $input = $('.form-reviews > input');
    $input.blur(function (e) {
        $(this).toggleClass('filled', !!$(this).val());
    $(".linkButton").click(function() {
        $( "input[name*='formInfo']" ).val($(this).attr( "title" ));

The problem is that if the form is not filled out correctly, then all form fields are cleared, although in theory the fields should only be cleared if the form is successfully submitted, what's wrong?
How to make it so that if one of the fields is filled out incorrectly, it is highlighted so that the user understands where the error is?
Glad for any help

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Alexey Yakhnenko, 2018-01-23

success / error callbacks - they are about the Ajax request itself, and not about the correctness of filling out the form.
at you at any successful (ie which reached the server and returned some answer) request the form will be cleared.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question