Liran Atli Liran Atli - 14 days ago 5
Ajax Question

trying to pass success or not from ajax to html login form

hi i am trying to pass login form but its give to me success all the time ,if its wrong user or user in data base , this is my code =>

this is the ajax code =>

$J(document).ready(function () {
$J("#add_err").css('display', 'none', 'important');
$J("#login").click(function () {
email = $J("#email").val();
password = $J("#password").val();
$J.ajax({
type: "POST",
url: "login.php",
data: "email=" + email + "&password=" + password,
success:function (data) {
if (data) {
//$("#add_err").html("right username or password");
window.location = "dashboard.php";
} else {
$J("#add_err").css('display', 'inline', 'important');
$J("#add_err").html("<img src='images/alert.png' /> <b>Wrong username or password </b>" );
}
},
beforeSend: function ()
{
$J("#add_err").css('display', 'inline', 'important');
$J("#add_err").html("<img src='images/spin.gif' /> Loading...")
}
});
return false;
});
});


this is the form => in file login.php

<form action="" method="POST" id="loginForm">
<div class="form-group has-feedback">
<input type="email" name="email" id="email" class="form-control" placeholder="Email">
<span class="glyphicon glyphicon-envelope form-control-feedback"></span>
</div>
<div class="form-group has-feedback">
<input type="password" name="password" id="password" class="form-control" placeholder="Password">
<span class="glyphicon glyphicon-lock form-control-feedback"></span>
</div>
<div class="row">
<div class="col-xs-8">
<div class="checkbox icheck">
<input type="checkbox" name="iCheck" />
<b>Remember me </b>
<script>
$J(document).ready(function () {
$J('input').iCheck({
checkboxClass: 'icheckbox_flat-blue',
radioClass: 'iradio_flat',
increaseArea: '-10%', // optional
});
});
</script>
</div>
</div>
<!-- /.col -->
<div class="col-xs-4">
<button type="submit" id="login" class="btn btn-primary btn-block btn-flat">Sign In</button>
</div>
<!-- /.col -->
</div>
</form>


and this is the post i move to data base => in same file login.php

if (isset($_POST['email']) && isset($_POST['password'])) {
session_start();
$dbConnect = new dbconnect();
$email = $_POST['email'];
$password = $_POST['password'];
$userData = $dbConnect->verify_login($email, $password);
if (!$userData) {
echo "false";
}}


varifylogin function=>

public function verify_login($email, $password) {
$this->Connect();
$sql = "SELECT email,password FROM `users_details` WHERE `email`='$email' AND `password`='$password' ";
if ($this->res = mysqli_query($this->ind_connect, $sql)) {
$num_row = mysqli_num_rows($this->res);
$row = mysqli_fetch_assoc($this->res);
if ($num_row == 1) {
echo 'true';
$_SESSION['email'] = $row['email'];
$_SESSION['password'] = $row['password'];
}
} else {
echo 'errr';
}
$this->Disconnect();
}


i realy do not know what to do
help is welcome now Ty.

Answer

In the function: verify_login

You must return a true value of boolean type, not as text.

Then improve the code as follows.

$userData = $dbConnect->verify_login($email, $password);
if ($userData) {
    echo "true";
}else{
    echo "false";
}
}

In ajax call

success:function (data) {
if (data=='true') {
//..
}else{
//..
}
}

Another way is to make php return a json, and in ajax it indicates dataType: "json"

$.ajax({
    url : "",
    type : "POST",
    dataType : 'json',
    success : function(data) {
        console.log(data);
        if(data.success==true){

        }else{
        }
   }
 });

In the file php:

echo json_encode(array('success'=>true));