My Form does not send

0

I made a form to send contact data (name, email, subject and message).

<form name="contactForm" id="form-contato" method="post">
                <fieldset>

                <div class="form-field">
                    <input name="contactName" type="text" id="Nome" name="Nome" placeholder="Seu nome" value="" minlength="2" maxlength="50" required="" aria-required="true" class="full-width">
                </div>
                <div class="form-field">
                    <input name="contactEmail" type="email" id="Email" name="Email" placeholder="Seu e-mail" value="" required="" aria-required="true" class="full-width" required="required" type="email">
                </div>
                <div class="form-field">
                    <input name="contactSubject" type="text" id="Assunto" name="Assunto" placeholder="Assunto" value="" class="full-width">
                </div>
                <div class="form-field">
                    <textarea name="contactMessage" id="Mensagem" name="Mensagem" placeholder="Sua mensagem" rows="10" cols="50" required="" aria-required="true" class="full-width"></textarea>
                </div>
                <div class="form-field">
                    <input class="full-width btn--primary" type="submit" value="Enviar">
                    <div class="submit-loader">
                        <div class="text-loader">Enviando...</div>
                        <div class="s-loader">
                            <div class="bounce1"></div>
                            <div class="bounce2"></div>
                            <div class="bounce3"></div>
                        </div>
                    </div>
                </div>

                </fieldset>
</form>

            <!-- contact-warning -->
            <div class="message-warning">
                Ops, aconteceu algum problema, atualize a página e tente novamente, obrigado!
            </div> 

            <!-- contact-success -->
            <div class="message-success">
                Mensagem enviada com sucesso, obrigado!<br>
            </div>

I am using a PHP script ( send_email ), but I can not trigger sending the form data. My script was inserted before the tag was closed

<script>
            // Variable to hold request
            var request;

            // Bind to the submit event of our form
            $("#form-contato").submit(function(event){

                // Prevent default posting of form - put here to work in case of errors
                event.preventDefault();

                // Abort any pending request
                if (request) {
                    request.abort();
                }
                // setup some local variables
                var $form = $(this);

                // Let's select and cache all the fields
                var $inputs = $form.find("input, select, button, textarea");

                // Serialize the data in the form
                var serializedData = $form.serialize();

                // Let's disable the inputs for the duration of the Ajax request.
                // Note: we disable elements AFTER the form data has been serialized.
                // Disabled form elements will not be serialized.
                $inputs.prop("disabled", true);

                // Fire off the request to /form.php
                request = $.ajax({
                    url: "send_email.php",
                    type: "post",
                    data: serializedData,
                    dataType: "json"
                });

                // Callback handler that will be called on success
                request.done(function (response, textStatus, jqXHR){
                    // Log a message to the console
                    console.log("Hooray, it worked!", response);
                });

                // Callback handler that will be called on failure
                request.fail(function (jqXHR, textStatus, errorThrown){
                    // Log the error to the console
                    console.error(
                        "The following error occurred: "+
                        textStatus, errorThrown
                    );
                });

                // Callback handler that will be called regardless
                // if the request failed or succeeded
                request.always(function () {
                    // Reenable the inputs
                    $inputs.prop("disabled", false);
                });

            });
</script>

This is the error generated in the console:

Can you help me?

    
asked by anonymous 15.03.2018 / 03:20

1 answer

0

As you can see, you're using an event trigger with jquery, if so, I'll post a code that always works with me, no problem;

    <form name="contactForm" id="form-contato" method="post">
            <fieldset>

            <div class="form-field">
                <input name="contactName" type="text" id="Nome" name="Nome" placeholder="Seu nome" value="" minlength="2" maxlength="50" required="" aria-required="true" class="full-width">
            </div>
            <div class="form-field">
                <input name="contactEmail" type="email" id="Email" name="Email" placeholder="Seu e-mail" value="" required="" aria-required="true" class="full-width" required="required" type="email">
            </div>
            <div class="form-field">
                <input name="contactSubject" type="text" id="Assunto" name="Assunto" placeholder="Assunto" value="" class="full-width">
            </div>
            <div class="form-field">
                <textarea name="contactMessage" id="Mensagem" name="Mensagem" placeholder="Sua mensagem" rows="10" cols="50" required="" aria-required="true" class="full-width"></textarea>
            </div>
            <div class="form-field">
                <input class="btn-action full-width btn--primary" type="submit" value="Enviar">
                <div class="submit-loader">
                    <div class="text-loader">Enviando...</div>
                    <div class="s-loader">
                        <div class="bounce1"></div>
                        <div class="bounce2"></div>
                        <div class="bounce3"></div>
                    </div>
                </div>
            </div>

            </fieldset>
        </form>

(I just added a button class to be heard by JS)

<input class="btn-action full-width btn--primary" type="submit" value="Enviar">

Ajax

 function callAjax() {

    let data = $('#form-contato').serialize();

    $.ajax({
        url: 'send_mail.php',
        data: data,
        type: 'POST',
        dataType: 'json', //Você pode alterar aqui pra outros retornos desejados
        beforeSend: function () {

            $('.submit-loader').show();

        },
        success: function (data) {

            //Seu código aqui

            $('.submit-loader').hide();
            return false; 
        },
        error: function (data) {
            console.log(data); // Para ver se deu algum erro
            $('.submit-loader').hide();
        }
    })
}

    let btn = document.getElementsByClassName('btn-action')[0];
    btn.addEventListener('click', callAjax);
    
15.03.2018 / 15:14