Nhoufy
Nhoufy

Reputation: 53

Uncaught SyntaxError: Unexpected token { function js

Can you help me, here is my problem :

Uncaught SyntaxError: Unexpected token {

I tried but it does not work.

My JS :

$(document).ready(function() {
    $("#submit").click(function {
        $.post('connexion.php', {
                login: $("#username").val();
                password: $("#password").val();
            },
            function(data) {
                if (data == 'Success') {

                    page HTML.
                    $("#resultat").html("<p>Success !</p>");
                } else {
                    $("#resultat").html("<p>Error</p>");
                }
            },
            'text'
        );
    });
}); 

Upvotes: 0

Views: 3350

Answers (2)

Alexey Usachov
Alexey Usachov

Reputation: 1374

You forgot about () after click function $("#submit").click(function () { .

And there is a ; instead of , in object passed as argument

{
login : $("#username").val(),
password : $("#password").val()
}

Has to be

<script>
  $(document).ready(function(){
      $("#submit").click(function(){
      $.post('connexion.php',
      {
      login : $("#username").val(),
      password : $("#password").val()
      },
      function(data){
      if ( data == 'Success' ){


      $("#resultat").html("<p>Success !</p>");
      }
      else
      {
      $("#resultat").html("<p>Error</p>");
      }
      },
      'text'
      );
      });
  });
</script>

Upvotes: 2

Ankit Agarwal
Ankit Agarwal

Reputation: 30739

You are missing function arguments in click function and since the function has no arguments add (). Also, you have used a semicolon ; in the object instead of a comma , for $.post payload. Change your code to this:

$("#submit").click(function(){
  $.post('connexion.php',
  {
    login : $("#username").val(),
    password : $("#password").val()
  },
  function(data){
    if ( data == 'Success' ){
      $("#resultat").html("<p>Success !</p>");
    }
    else
    {
      $("#resultat").html("<p>Error</p>");
    }
  },
  'text'
  );
});

$("#submit").click(function(){
  $.post('connexion.php',
  {
    login : $("#username").val(),
    password : $("#password").val()
  },
  function(data){
    if ( data == 'Success' ){
      $("#resultat").html("<p>Success !</p>");
    }
    else
    {
      $("#resultat").html("<p>Error</p>");
    }
  },
  'text'
  );
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Upvotes: 2

Related Questions