0

here is my code

  $('#login').on('click',function(e){
    e.preventDefault();
    var username = document.getElementById('username').value;
    var password = document.getElementById('password').value;
    var ajax = new XMLHttpRequest();
    var vars = 'username='+username+'&password='+password;
    var url = "singInDrProcess.php";
    ajax.open("POST", url, true);
    ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    ajax.send(vars);
    ajax.onreadystatechange = function() {
        if(ajax.readyState == 4 && ajax.status == 200) {
            var return_data = ajax.responseText;
            if(return_data!="success_login")
                document.getElementById("result_signin").innerHTML = return_data;
            }else{
                window.location.replace("profile.php");
                           }
        }
  });

The problem is , when user comes to this page and tries to Login, even if he never enters any username or password, and just clicks on my login button, page will be redirected to profile.php I mean, the ELSE part always runs, either there is data on fields or all of them are empty what is the problem?

2
  • what a shame:( thank u but still not working after clicking on lonig, it goes directly to "singInDrProcess.php" instead of "profile.php", and ofcourse with a "Object not found " error, but both profile.php and signInDrprocess.php are in my folders and working ??? Commented Feb 17, 2014 at 14:25
  • give try with adding full url of your page in replace function like window.location.replace("http://example.com/profile.php); Commented Feb 17, 2014 at 14:40

3 Answers 3

2

Indent your code according to your parenthesis:

if(ajax.readyState == 4 && ajax.status == 200) {
    var return_data = ajax.responseText;
    if(return_data!="success_login")
        document.getElementById("result_signin").innerHTML = return_data;
}else{
     window.location.replace("profile.php");
}
} // extraneous

That's not what you want.

Adding a { after if(return_data!="success_login") or removing the } before elsewill fix it:

if(ajax.readyState == 4 && ajax.status == 200) {
    var return_data = ajax.responseText;
    if (return_data!="success_login") {
        document.getElementById("result_signin").innerHTML = return_data;
    } else {
        window.location.replace("profile.php");
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

You forgot { after if(return_data!="success_login")

but should be

 if(ajax.readyState == 4 && ajax.status == 200) {
 //--------------add opening curly brace here---^

Comments

1
if(return_data!="success_login")
            document.getElementById("result_signin").innerHTML = return_data;
        }else{
            window.location.replace("profile.php");
                       }
    }
});

should be changed to

if(return_data!="success_login")
            {document.getElementById("result_signin").innerHTML = return_data;
        }else{
            window.location.replace("profile.php");
                       }
    }
});

You forgot the { after the if parentheses.

Also, you should use JSHint, as it helps with errors and such.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.