Submit does not call function

Asked

Viewed 659 times

1

After changing the code, I left this way but I analyzed the browser console and realized that the Submit button still not calling the function:

<div class="container">
<div class="spacer">
<div class="row contact">
<div class="col-lg-6 col-sm-6 ">

<form id="form-ajax">
        <input type="text" class="form-control" placeholder="Nome completo:" id="nome">
        <input type="text" class="form-control" placeholder="E-mail:" id="email">
        <input type="text" class="form-control" placeholder="Número para contato:" id="num">
        <textarea rows="6" class="form-control" placeholder="Digite sua mensagem" id="msg"></textarea>
        <input type="button" class="btn btn-success" id="enviar" value="Enviar">
</form>
</div>
</div>
</div>
</div>

<script>
$(document).ready(function(){
    $('#form-ajax').click(function(e){  
    e.preventDefault();
    alert('entrou na função');
    if($('#enviar').val() === 'Enviando...'){
    return(false);
    }

    $('#enviar').val('Enviando...');

    $.ajax({
        url: 'phpmailer.php',
        type: 'post',
        dataType: 'json',
        data: {
            'nome':$('#nome').val(),
            'email':$('#email').val(),
            'num':$('#num').val(),
            'msg':$('#msg').val()
            }
    }).done(function(data){

        alert(data);
        $('#nome').val();
        $('#email').val();
        $('#num').val();
        $('#msg').val();

    });
});
 });
</script>
  • Have you tried calling the function on the button?

4 answers

3

pass the jquery script to head if you don’t have it yet.

then put the

$(document).ready(function(){
    //...o seu script atual aqui   
});

and finally pass the e.preventDefault(); just below the

$('#form-ajax').submit(function(e) {
     e.preventDefault();
  • Already done, the strange thing is when I press the Submit button it returns to the page cleaning the fields, but without entering the script

  • Then swap Submit for a button. and call instead of Submit() the click()

  • I edited the post see how the code looked. Now when I click on the form button nothing happens.

  • this almost ;) now exchange form_ajax for sending $('#send'). click(Function(e){

  • I changed and again nothing happened when I click the button

  • Put the whole page code in a gist and send me the link

  • https://gist.github.com/ricardofeller/d18c54ba47d16d013e56ea5b466e1035

  • First you miss an equal in the forum id. 2 you can follow see the alert?

  • I can’t see the alert, I fixed the same one that was missing

Show 4 more comments

1

The problem is that the jQuery script runs before the page is fully loaded so it cannot assign the event to the form as it does not exist yet. You can solve this in 2 ways:

1 - You bring the script to the end of the page.

2 - Put any script inside jQuery’s "ready" function to say that the script should only be executed after the page has been loaded, like this:

$(document).ready(function(){
    //...o seu script atual aqui
});

1

Insert your script into the function below.

$(document).ready(function(){
    // Aqui
});

This will cause your script to run only after loading the page. A tip: always put your scripts after the content of the pages, before closing the body tag to prevent them from blocking the page loading for possible errors in the script.

1

<script type="text/javascript">
jQuery(function($) {
    $('#form-ajax').submit(function(e){
           alert('entrou na função');
    if($('#enviar').val() === 'Enviando...'){
           return(false);
    }$('#enviar').val('Enviando...');

    $.ajax({
        url: 'phpmailer.php',
        type: 'post',
        dataType: 'json',
        data: {
            'nome':$('#nome').val(),
            'email':$('#email').val(),
            'num':$('#num').val(),
            'msg':$('#msg').val()
            }
    }).done(function(data){

        alert(data);
        $('#nome').val();
        $('#email').val();
        $('#num').val();
        $('#msg').val();

    });
    e.preventDefault();
     });
 });
</script>

See if this works, don’t forget the Jquery library, before the script.

Browser other questions tagged

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