php - else not working in javascript
418
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?
Answer
Solution:
Indent your code according to your parenthesis:
That's not what you want.
Adding a
{-code-2}
after{-code-3}
or removing the}
beforeelse
will fix it:Answer
Solution:
You forgot
after
{-code-2}
but should be
Answer
Answer
Answer
Solution:
should be changed to
You forgot the { after the if parentheses.
Also, you should use JSHint, as it helps with errors and such.