My Form does not send

Asked

Viewed 1,021 times

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’m using a script PHP (send_email), but I can’t trigger the submission form. My script was inserted before closing the tag

<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:

Console Error - jquery

Can you help me?

  • You didn’t define the attribute action of form, so the HTML is being sent to the page itself, if this is not the problem clarify your doubt

  • Maybe the action is missing pointing to the page in the form and the method, also in the form with the type of submission. If possible, put the script in PHP so we can help in a better way.

1 answer

0

From what you can see, you are using an event trigger with jquery, in case that is, I will post a code that always works with me, no problems;

    <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 class to the button to be heard by the JS )

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

Who "activates" the form will be like this Ajax, so the code will be like this:

 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);
  • But where do I put this callAjax?

  • I’ll edit with my more detailed code.

  • I made the change, this is the mode in which my form is and below the script that calls the function...

  • On the last line, buddy, it goes like this: btn.addeventlistener('click', callAjax); that’s what you’ll call your callAjax

  • Excuse the ignorance but I must add the callAjax() Function below the form or at the bottom of the page?

  • There is no ignorance, you are learning. you can put at the end of the page, after calling the Javascript file that contains the function, or you can put in the Javascript file itself. Just so you understand what happens there; the btn Let = Document.getElementsByClassName('btn-action')[0]; you are "picking up" the button. btn.addeventlistener('click', callAjax); you are saying "When someone clicks on it, activate callAjax". That is, you need these two lines, if you want you can remove the function and just put with $('. btn-action'). on('click', Function(){ // put everything you need in here.})

  • I still have the same problem, put the call ajax Function before closing my <script>, but nothing...

  • I believe that maybe there may be some problem in my classes that register at the bank, or the ones that actually fire the email... but I think the best thing is that I redo everything and guide myself with other documentation...

  • Now I noticed, realized that you are using the attribute NAME twice in each input, you can only use once for input: <input name="contactName" type="text" id="Name" name="Name" placeholder="Your name" value="" minlength="2" maxlength="50" required="" Aria-required="true" class="full-width">, remove from all and leave only 1 NAME per INPUT

  • I corrected both Nos, but still nothing...

  • Redo the form, carefully, there is a lot of duplicated stuff, the TYPE is too! This error seems to be that you sending JSON but actually has HTML in the content, hence the problem

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.