Im building simple login system using Ajax XMLhttpRequest. PHP File and Javascript all working fine.. but when i use response test in IF Else Condition its not working as i expect.
Here My HTML
<div class="login-form">
<input type="username" name="username" id="username" class="text-input--underbar" placeholder="Username" value="">
<input type="password" name="password" id="password" class="text-input--underbar" placeholder="Password" value="">
<br><br>
<ons-button modifier="large" onClick="javascript:ajax_post();" class="login-button">Log In</ons-button>
<br><br>
<ons-button modifier="quiet" class="forgot-password">Forgot password?</ons-button>
</div>
<div id="status"></div>
Here my Javascript Code.
function ajax_post(){
// Create our XMLHttpRequest object
var hr = new XMLHttpRequest();
// Create some variables we need to send to our PHP file
var url = "http://boost.meximas.com/mobile/login.php";
var fn = document.getElementById("username").value;
var ln = document.getElementById("password").value;
var vars = "username="+fn+"&password="+ln;
hr.open("POST", url, true);
// Set content type header information for sending url encoded variables in the request
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
// Access the onreadystatechange event for the XMLHttpRequest object
hr.onreadystatechange = function() {
if(hr.readyState == 4 && hr.status == 200) {
var return_data = hr.responseText;
document.getElementById("status").innerHTML = return_data;
if(return_data=="true"){
alert("Yes Login True");
}else{
alert("No Login False");
}
}
}
// Send the data to PHP now... and wait for response to update the status div
hr.send(vars); // Actually execute the request
document.getElementById("status").innerHTML = "processing...";
}
Here my PHP Code
if(isset($_POST['username']) && isset($_POST['password'])){
$username = $_POST['username'];
$password = $_POST['password'];
}
$sql = "SELECT * FROM member WHERE username = '$username' AND password = '$password'";
$result = mysqli_query($con,$sql);
if(!$result){
echo "failed";
}else{
echo "true";
}
For the Wrong input also im getting Yes Login True
and status inner HTML True
that mean PHP file always returning True. but when i check php file alone it works fine.. there is no errors.
i meant using this.
while($row = mysqli_fetch_array($result)) {
echo "Hello" .$row['email']. "Thanks";
}
it gives correct output.
Im sorry if its unclear.. please let me know.