Pado
Pado

Reputation: 41

jQuery and AJAX work only once

I have problem with AJAX and jQuery. I write function for login to system, but it works only first time. Here is my code:

html in modal:

<form role="form" onsubmit=" return login()"  method="post" action="" >
   <div class="form-group">
     <label for="userName"><span class="glyphicon glyphicon-user"></span>E-mail</label>
     <input type="email" name="emailLogin" class="form-control" id="userName" placeholder="e-mail" required>
   </div>
   <div class="form-group">
      <label for="password"><span class="glyphicon glyphicon-eye-open"><span>Password</label>
      <input type="password" name="passLogin" class="form-control" id="password" placeholder="Password" required>
    </div>
    <button type="submit" class="btn btn-success btn-block">Login<span class="glyphicon glyphicon-log-in"></span></button>
</form>

here is jquery:

function login(){
  login=document.getElementById('userName').value;
  pass=document.getElementById('password').value;
  var dataString="emailLogin="+login+"&passLogin="+pass;

  $.ajax({
    type: "POST",
    url: "models/handler/KlientHandler.php",
    cache: false,
    data: dataString,
    success: function(text){
      if(text=='0'){
        $("#loginError").removeClass('hidden');
      }else{
        $("#loginOk").removeClass('hidden');
        $("#myModal").modal('hide');
        $("#loginLi").html("<a id=\"user\">"+login+" (Profile)<span class=\"glyphicon glyphicon-user\"></span></a>");
        $("#regLi").html("<a href=\"logout\" id=\"user\">"+login+" (Logout)<span class=\"glyphicon glyphicon-log-out\"></span></a>");
      }
    }
  });
  return false;
}

Upvotes: 0

Views: 301

Answers (2)

Adam Love
Adam Love

Reputation: 656

The snippet tests your logic and there was a console error for your login and pass variables. I set them as 'var' and put your success logic in the 'error' section of the ajax request since I cannot reach your server, but it does create the logout button. Is that what you wanted?

function login() {
    var login = document.getElementById('userName').value;
    var pass = document.getElementById('password').value;
    var dataString = "emailLogin=" + login + "&passLogin=" + pass;

    $.ajax({
        type: "POST",
        url: "models/handler/KlientHandler.php",
        cache: false,
        data: dataString,
        success: function(text) {
          /* Success logic */
        },
        error: function() {
                alert('Test - success logic');
                $("#loginOk").removeClass('hidden');
                //$("#myModal").modal('hide');
                $("#loginLi").html("<a id=\"user\">" + login + " (Profile)<span class=\"glyphicon glyphicon-user\"></span></a>");
                $("#regLi").html("<a href=\"logout\" id=\"user\">" + login + " (Logout)<span class=\"glyphicon glyphicon-log-out\"></span></a>");
        }

    });
    return false;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form role="form" onsubmit=" return login()"  method="post" action="" >
   <div class="form-group">
     <label for="userName"><span class="glyphicon glyphicon-user"></span>E-mail</label>
     <input type="email" name="emailLogin" class="form-control" id="userName" placeholder="e-mail" required>
   </div>
   <div class="form-group">
      <label for="password"><span class="glyphicon glyphicon-eye-open"><span>Password</label>
      <input type="password" name="passLogin" class="form-control" id="password" placeholder="Password" required>
    </div>
    <button type="submit" class="btn btn-success btn-block">Login<span class="glyphicon glyphicon-log-in"></span></button>
</form>

<div id="loginLi"></div>
<div id="regLi"></div>
<div id="loginOk">Login OK</div>

Upvotes: 0

Kevin B
Kevin B

Reputation: 95031

You are overwriting your login function with a string.

function foo() {
  alert('foo');
  foo = function () {
    alert('bar');
  }
}
<a onclick="foo()">
click me!
</a>

See how the second click here causes a different alert? In your case you're replacing the function with a string instead of a function, causing a syntax error. Don't forget to var your variables.

var login=document.getElementById('userName').value;
var pass=document.getElementById('password').value;

Upvotes: 2

Related Questions