Problem with validation of several forms with ajax and php

1

On my site I have several forms: real estate filter form, contact form, form talk to the president and the form of suggestions.

Is there any way to retrieve all forms except the real estate filter form and send to different urls in ajax?

In the code below I'm working only with a form.

$(document).ready(function(){
        //Forms
        var formMsg     = $('.load');
        var button      = $('#btnEnvia'); 
        var forms       = $('form');
        var has_error   = 0 ;
        var nome        = $("#nome").val();
        var msg         = $("#msg").val();
        var email       = $("#email").val();
        var telefone    = $("#telefone").val();
        var atpos       = email.indexOf("@");
        var dotpos      = email.lastIndexOf(".");

        var urlpost = 'tpl/form-contato.php';
        var urlpost2 = 'tpl/form-presidente.php';
        var urlpost3 = 'tpl/form-contato.php';

        formMsg.hide();
        button.attr("type", "submit");
        forms.submit(function(){
            formMsg.fadeOut("fast");
            return false;
        });

        function carregando(){
            formMsg.empty().html('<p><img src="img/load.gif" width="22">  Agurade enviando...</p>').fadeIn("fast");
        }

        function errosend(){
            formMsg.empty().html('<p class="danger">Erro inesperado, contate o administrador.</p>').fadeIn("fast");
        }

        //Genericas
        function errdados( mensagem ){
            formMsg.empty().html('<p class="bg-danger">'+mensagem+'</p>').fadeIn("fast");
        }
        function sucesso( mensagem ){
            formMsg.empty().html('<p class="success">'+mensagem+'</p>').fadeIn("fast");
        }

        $.ajaxSetup({
            url:        urlpost,
            type:       'POST',
            beforeSend: carregando,
            error:      errosend
        });


        var formContato = $('form[name="contato"]');

        formContato.submit(function(){
            var dados = $(this).serialize();
            var acao = "&acao=enviar"
            if ($("#nome").val().length == 0) {
                has_error = 1 ;
                $('#nome').css({
                    "background-color": "rgba(248, 116, 116, 0.52)"
            });
            }
            if($("#email").val().length == 0) {
                has_error = 1 ;
                $('#email').css({
                    "background-color": "rgba(248, 116, 116, 0.52)"
                });
            }
            if(atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= email.length) {
                has_error = 1 ;
                $('#email').css({
                    "background-color": "rgba(248, 116, 116, 0.52)"
                });
            }
            if($("#msg").val().length == 0) {
                has_error = 1 ;
                $('#msg').css({
                    "background-color": "rgba(248, 116, 116, 0.52)"
                });
            }
            if(has_error == 0 ) {
                $.ajax({
                    data: dados+acao,
                    success: function(resposta){
                        sucesso("Enviado com sucesso!");
                        $('#btnEnvia').val('ENVIADO!');
                         $('#btnEnvia').css({
                            "background-color": "#00E681"
                        });
                    }
                });
            }
        });
    
asked by anonymous 23.08.2015 / 22:37

1 answer

1

For Jquery 1.5+ , you can use the object:

$.when($.ajax(), [...]).then(function(resultado){},[...]);

Example:

<input type="button" id="button" value="enviar" />

$('#button').click(function() {
    $.when(
        $.ajax({
            url: 'tpl/form-contato.php',
            success: function(data) {
                alert('Deu certo!')
            }
        }),
        $.ajax({
            url: 'tpl/form-presidente.php',
            success: function(data) {
                alert('Deu certo!')
            }
        }),
        $.ajax({
            url: 'tpl/form-contato.php',
            success: function(data) {
                alert('Deu certo!')
            }
        })
    ).then( function(){
        alert('Deu TUDO certo!');
    });
});
  

So, a single Deferred is passed to jQuery.when ();

Font

    
25.08.2015 / 02:06