jQuery click() is not running

Asked

Viewed 1,619 times

-2

I’m trying to do a function in jQuery that by clicking on button he executed the function click() jQuery to take input data and send via post.

HTML

<input type="text" name="nome" class="input-xlarge" placeholder="Nome" required="required" />
<input type="email" name="email"  class="input-xlarge" placeholder="Endereço de email" required="required" />
<input type="text" name="dominio" class="input-xlarge" placeholder="Domínio" required="required" />
<button type="submit" class="btn submit" id="submit">Criar uma conta</button>

jQuery

<script>
    $(document).ready(function()
    {
        $("#submit").click(function()
        {
            var nome = $("input[name=nome]").val();
            var email = $("input[name=email]").val();
            var dominio = $("input[name=dominio]").val();
            $.post('api.php', {nome:nome, email:email}, function(data)
            {   
                alert(data);    
            })
        })
    })
</script>

In the browser console an error appears:

Uncaught Referenceerror: Datex is not defined preco.php:29 (Anonymous Function) preco.php:29 (Anonymous Function) preco.php:30 Uncaught Referenceerror: _gaq is not defined global.js:1 (Anonymous Function) global.js:1 o jquery.min.js:2 p.fireWith jquery.min.js:2 e.extend.ready jquery.min.js:2 c.addEventListener.B

  • It doesn’t return anything to me. I click the button and nothing happens. It doesn’t even show Alert()

  • Has a <form> around those fields or not? If so, include the code in the question.

  • Has no <form>.

  • The error seems in php now :(

  • 2

    To clarify why I closed this question: the author commented in the answer below that there was a problem in the upload order of the js files. I considered it unlikely that another person would have the same problem with the same symptom (the above error).

1 answer

4


Try that code:

$(document).ready(function(){

    $("#submit").click(function(event){

        event.stopPropagation();

        var nome = $("input[name=nome]").val();
        var email = $("input[name=email]").val();
        var dominio = $("input[name=dominio]").val();

        $.post('api.php', {nome:nome, email:email}, function(data){

            alert(data);
        });

        return false;
    });
});

The problem happens because your button has type="submit". The browser understands that by clicking this button the user will be submitting information to the server.

Using the event.stopPropagation() vc ensures that the event does not propagate.

The return false at the end of the function it is necessary to stop processing the event (read this)

  • Still not returning anything. Just click and nothing happens, even using type as button or putting <input type="button">

  • I edited the code. Try to put return false; at the end of callback function.

  • Nothing, still no action going on;

  • 1

    I just read what you edited in the question, it seems you have an error in your code 'api.php' (server-side). Vc can debug by doing console.log("algum texto"); to see how far the javascript (client-side) code is going. I suggest putting a log before and after your call $.post.

  • 2

    I managed to fix it. I put the JS code under jquery.js

Browser other questions tagged

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